<!DOCTYPE html>
<html lang="en-CN" dir="ltr">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>简述 | 阿飞博客</title>
    <meta name="description" content="Vite & Vue powered static site generator.">
    <link rel="preload stylesheet" href="/Blogs/assets/style.a20661eb.css" as="style">
    <link rel="modulepreload" href="/Blogs/assets/app.2f47ee87.js">
    <link rel="modulepreload" href="/Blogs/assets/MySqlStudy_MySQL.md.113ea88e.lean.js">
    
    <link rel="icon" href="/planet.svg">
  <script id="check-dark-light">(()=>{const e=localStorage.getItem("vitepress-theme-appearance")||"",a=window.matchMedia("(prefers-color-scheme: dark)").matches;(!e||e==="auto"?a:e==="dark")&&document.documentElement.classList.add("dark")})();</script>
  </head>
  <body>
    <div id="app"><div class="Layout" data-v-93a960b4><!--[--><!--]--><!--[--><span tabindex="-1" data-v-151f2593></span><a href="#VPContent" class="VPSkipLink visually-hidden" data-v-151f2593> Skip to content </a><!--]--><!----><header class="VPNav" data-v-93a960b4 data-v-0fa0e57d><div class="VPNavBar has-sidebar" data-v-0fa0e57d data-v-be450ad9><div class="container" data-v-be450ad9><div class="title" data-v-be450ad9><div class="VPNavBarTitle has-sidebar" data-v-be450ad9 data-v-6d2fb2d9><a class="title" href="/Blogs/" data-v-6d2fb2d9><!--[--><!--]--><!--[--><img class="VPImage logo" src="/Blogs/planet.svg" alt data-v-6db2186b><!--]--><!--[-->My Study Blog 💓 ❤️‍🔥<!--]--><!--[--><!--]--></a></div></div><div class="content" data-v-be450ad9><div class="curtain" data-v-be450ad9></div><div class="content-body" data-v-be450ad9><!--[--><!--]--><!----><nav aria-labelledby="main-nav-aria-label" class="VPNavBarMenu menu" data-v-be450ad9 data-v-bdedfc22><span id="main-nav-aria-label" class="visually-hidden" data-v-bdedfc22>Main Navigation</span><!--[--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Blogs/" data-v-bdedfc22 data-v-f2ec7ecf data-v-a8b5c975><!--[-->主页<!--]--><!----></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="../whyVitePress/index.html" data-v-bdedfc22 data-v-f2ec7ecf data-v-a8b5c975><!--[-->学习笔记<!--]--><!----></a><!--]--><!--[--><div class="VPFlyout VPNavBarMenuGroup" data-v-bdedfc22 data-v-96001b6b><button type="button" class="button" aria-haspopup="true" aria-expanded="false" data-v-96001b6b><span class="text" data-v-96001b6b><!----> 文章 <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="text-icon" data-v-96001b6b><path d="M12,16c-0.3,0-0.5-0.1-0.7-0.3l-6-6c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l5.3,5.3l5.3-5.3c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-6,6C12.5,15.9,12.3,16,12,16z"></path></svg></span></button><div class="menu" data-v-96001b6b><div class="VPMenu" data-v-96001b6b data-v-e7ea1737><div class="items" data-v-e7ea1737><!--[--><!--[--><div class="VPMenuLink" data-v-e7ea1737 data-v-88f937c6><a class="VPLink link" href="/Blogs/article/" data-v-88f937c6 data-v-a8b5c975><!--[-->某一时刻的心得<!--]--><!----></a></div><!--]--><!--[--><div class="VPMenuLink" data-v-e7ea1737 data-v-88f937c6><a class="VPLink link" href="/Blogs/article/2022.5.2.html" data-v-88f937c6 data-v-a8b5c975><!--[-->想念月色<!--]--><!----></a></div><!--]--><!--]--></div><!--[--><!--]--></div></div></div><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink active" href="/Blogs/MySqlStudy/MySQL.html" data-v-bdedfc22 data-v-f2ec7ecf data-v-a8b5c975><!--[-->数据库<!--]--><!----></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Blogs/Toolweb/" data-v-bdedfc22 data-v-f2ec7ecf data-v-a8b5c975><!--[-->常用网站<!--]--><!----></a><!--]--><!--[--><a class="VPLink link VPNavBarMenuLink" href="/Blogs/about/" data-v-bdedfc22 data-v-f2ec7ecf data-v-a8b5c975><!--[-->关于<!--]--><!----></a><!--]--><!--]--></nav><!----><div class="VPNavBarAppearance appearance" data-v-be450ad9 data-v-da3f667a><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" aria-label="toggle dark mode" aria-checked="false" data-v-da3f667a data-v-0d529b6d data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-0d529b6d><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-0d529b6d><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div><div class="VPSocialLinks VPNavBarSocialLinks social-links" data-v-be450ad9 data-v-2ab2a029 data-v-f6988cfb><!--[--><a class="VPSocialLink" href="https://github.com/SunFei123456" target="_blank" rel="noopener" data-v-f6988cfb data-v-e57698f6><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><!--]--></div><div class="VPFlyout VPNavBarExtra extra" data-v-be450ad9 data-v-66bb1f24 data-v-96001b6b><button type="button" class="button" aria-haspopup="true" aria-expanded="false" aria-label="extra navigation" data-v-96001b6b><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="icon" data-v-96001b6b><circle cx="12" cy="12" r="2"></circle><circle cx="19" cy="12" r="2"></circle><circle cx="5" cy="12" r="2"></circle></svg></button><div class="menu" data-v-96001b6b><div class="VPMenu" data-v-96001b6b data-v-e7ea1737><!----><!--[--><!--[--><!----><div class="group" data-v-66bb1f24><div class="item appearance" data-v-66bb1f24><p class="label" data-v-66bb1f24>Appearance</p><div class="appearance-action" data-v-66bb1f24><button class="VPSwitch VPSwitchAppearance" type="button" role="switch" aria-label="toggle dark mode" aria-checked="false" data-v-66bb1f24 data-v-0d529b6d data-v-f3c41672><span class="check" data-v-f3c41672><span class="icon" data-v-f3c41672><!--[--><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="sun" data-v-0d529b6d><path d="M12,18c-3.3,0-6-2.7-6-6s2.7-6,6-6s6,2.7,6,6S15.3,18,12,18zM12,8c-2.2,0-4,1.8-4,4c0,2.2,1.8,4,4,4c2.2,0,4-1.8,4-4C16,9.8,14.2,8,12,8z"></path><path d="M12,4c-0.6,0-1-0.4-1-1V1c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,3.6,12.6,4,12,4z"></path><path d="M12,24c-0.6,0-1-0.4-1-1v-2c0-0.6,0.4-1,1-1s1,0.4,1,1v2C13,23.6,12.6,24,12,24z"></path><path d="M5.6,6.6c-0.3,0-0.5-0.1-0.7-0.3L3.5,4.9c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C6.2,6.5,5.9,6.6,5.6,6.6z"></path><path d="M19.8,20.8c-0.3,0-0.5-0.1-0.7-0.3l-1.4-1.4c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l1.4,1.4c0.4,0.4,0.4,1,0,1.4C20.3,20.7,20,20.8,19.8,20.8z"></path><path d="M3,13H1c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S3.6,13,3,13z"></path><path d="M23,13h-2c-0.6,0-1-0.4-1-1s0.4-1,1-1h2c0.6,0,1,0.4,1,1S23.6,13,23,13z"></path><path d="M4.2,20.8c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C4.7,20.7,4.5,20.8,4.2,20.8z"></path><path d="M18.4,6.6c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l1.4-1.4c0.4-0.4,1-0.4,1.4,0s0.4,1,0,1.4l-1.4,1.4C18.9,6.5,18.6,6.6,18.4,6.6z"></path></svg><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="moon" data-v-0d529b6d><path d="M12.1,22c-0.3,0-0.6,0-0.9,0c-5.5-0.5-9.5-5.4-9-10.9c0.4-4.8,4.2-8.6,9-9c0.4,0,0.8,0.2,1,0.5c0.2,0.3,0.2,0.8-0.1,1.1c-2,2.7-1.4,6.4,1.3,8.4c2.1,1.6,5,1.6,7.1,0c0.3-0.2,0.7-0.3,1.1-0.1c0.3,0.2,0.5,0.6,0.5,1c-0.2,2.7-1.5,5.1-3.6,6.8C16.6,21.2,14.4,22,12.1,22zM9.3,4.4c-2.9,1-5,3.6-5.2,6.8c-0.4,4.4,2.8,8.3,7.2,8.7c2.1,0.2,4.2-0.4,5.8-1.8c1.1-0.9,1.9-2.1,2.4-3.4c-2.5,0.9-5.3,0.5-7.5-1.1C9.2,11.4,8.1,7.7,9.3,4.4z"></path></svg><!--]--></span></span></button></div></div></div><div class="group" data-v-66bb1f24><div class="item social-links" data-v-66bb1f24><div class="VPSocialLinks social-links-list" data-v-66bb1f24 data-v-f6988cfb><!--[--><a class="VPSocialLink" href="https://github.com/SunFei123456" target="_blank" rel="noopener" data-v-f6988cfb data-v-e57698f6><svg role="img" viewBox="0 0 24 24" xmlns="http://www.w3.org/2000/svg"><title>GitHub</title><path d="M12 .297c-6.63 0-12 5.373-12 12 0 5.303 3.438 9.8 8.205 11.385.6.113.82-.258.82-.577 0-.285-.01-1.04-.015-2.04-3.338.724-4.042-1.61-4.042-1.61C4.422 18.07 3.633 17.7 3.633 17.7c-1.087-.744.084-.729.084-.729 1.205.084 1.838 1.236 1.838 1.236 1.07 1.835 2.809 1.305 3.495.998.108-.776.417-1.305.76-1.605-2.665-.3-5.466-1.332-5.466-5.93 0-1.31.465-2.38 1.235-3.22-.135-.303-.54-1.523.105-3.176 0 0 1.005-.322 3.3 1.23.96-.267 1.98-.399 3-.405 1.02.006 2.04.138 3 .405 2.28-1.552 3.285-1.23 3.285-1.23.645 1.653.24 2.873.12 3.176.765.84 1.23 1.91 1.23 3.22 0 4.61-2.805 5.625-5.475 5.92.42.36.81 1.096.81 2.22 0 1.606-.015 2.896-.015 3.286 0 .315.21.69.825.57C20.565 22.092 24 17.592 24 12.297c0-6.627-5.373-12-12-12"/></svg></a><!--]--></div></div></div><!--]--><!--]--></div></div></div><!--[--><!--]--><button type="button" class="VPNavBarHamburger hamburger" aria-label="mobile navigation" aria-expanded="false" aria-controls="VPNavScreen" data-v-be450ad9 data-v-e5dd9c1c><span class="container" data-v-e5dd9c1c><span class="top" data-v-e5dd9c1c></span><span class="middle" data-v-e5dd9c1c></span><span class="bottom" data-v-e5dd9c1c></span></span></button></div></div></div></div><!----></header><div class="VPLocalNav" data-v-93a960b4 data-v-2817d72e><button class="menu" aria-expanded="false" aria-controls="VPSidebarNav" data-v-2817d72e><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="menu-icon" data-v-2817d72e><path d="M17,11H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,11,17,11z"></path><path d="M21,7H3C2.4,7,2,6.6,2,6s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,7,21,7z"></path><path d="M21,15H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h18c0.6,0,1,0.4,1,1S21.6,15,21,15z"></path><path d="M17,19H3c-0.6,0-1-0.4-1-1s0.4-1,1-1h14c0.6,0,1,0.4,1,1S17.6,19,17,19z"></path></svg><span class="menu-text" data-v-2817d72e>Menu</span></button><a class="top-link" href="#" data-v-2817d72e>Return to top</a></div><aside class="VPSidebar" data-v-93a960b4 data-v-c79ccefa><div class="curtain" data-v-c79ccefa></div><nav class="nav" id="VPSidebarNav" aria-labelledby="sidebar-aria-label" tabindex="-1" data-v-c79ccefa><span class="visually-hidden" id="sidebar-aria-label" data-v-c79ccefa> Sidebar Navigation </span><!--[--><!--]--><!--[--><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>为什么选择vitePress搭建</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../whyVitePress/index.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>介绍</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../whyVitePress/start.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>快速开始</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>每日记录</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../DailyLife/2023-03.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>2023年三月份</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>HTML 部分</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../HTML/SemanticTagging.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>语义化标签</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../HTML/Rearrange-and-redraw.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>重排和重绘</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../HTML/index.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>面试系列</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>CSS 部分</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../CssStudy/boxModel.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>盒子模型</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../CssStudy/boxJuzhong.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>盒子水平居中的方式有哪些</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../CssStudy/flex.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>Flex布局详解</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../CssStudy/CssDaoYing.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>CSS倒影实现</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../CssStudy/CssOptimization.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>CSS优化方式</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>JS 部分</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../JsStudy/Array.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>数组</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../JsStudy/KonwElType.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>确定js里的数据的类型</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../JsStudy/Fd-and-JieLiu.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>防抖与节流</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../JsStudy/3-7-ObjectDeconstruction.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>ES6 新增 对象解构语法</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><div class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>AJAX 部分</p><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><!----></div></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>Vue 系列</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../VueStudy/1.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>Vue2和Vue3 的区别</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../VueStudy/Vue.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>Vue2 基础部分的学习</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>小程序开发系列</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../whyVitePress/introduction.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>语义化标签</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>第三方库系列</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../JsLibrary/gsap.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>GSAP 绿袜子</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>Web3D系列</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../Web3D/three.js.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>ThreeJs3D开发</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>前端常用工具网站</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../Toolweb/index.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>ICON 图标库</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../Toolweb/index.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>前端Ui库</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>数据库</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../MySqlStudy/MySQL.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>MySql 基础部分</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>VsCode插件推荐</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../VsCodeChajian/index.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>一些有趣好玩的插件</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>某一时刻的心得</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../article/index.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>心念 不安</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../article/2022.5.2.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>愿希 君安</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><div class="group" data-v-c79ccefa><section class="VPSidebarItem level-0 collapsible collapsed" data-v-c79ccefa data-v-983f6b21><div class="item" role="button" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link" data-v-983f6b21 data-v-a8b5c975><!--[--><h2 class="text" data-v-983f6b21>学习摄影-拍照</h2><!--]--><!----></a><div class="caret" role="button" data-v-983f6b21><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" viewbox="0 0 24 24" class="caret-icon" data-v-983f6b21><path d="M9,19c-0.3,0-0.5-0.1-0.7-0.3c-0.4-0.4-0.4-1,0-1.4l5.3-5.3L8.3,6.7c-0.4-0.4-0.4-1,0-1.4s1-0.4,1.4,0l6,6c0.4,0.4,0.4,1,0,1.4l-6,6C9.5,18.9,9.3,19,9,19z"></path></svg></div></div><div class="items" data-v-983f6b21><!--[--><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../PhotoTips/1.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>要掌握的拍照几大技巧</p><!--]--><!----></a><!----></div><!----></div><div class="VPSidebarItem level-1 is-link" data-v-983f6b21 data-v-983f6b21><div class="item" data-v-983f6b21><div class="indicator" data-v-983f6b21></div><a class="VPLink link link" href="../PhotoTips/2.html" data-v-983f6b21 data-v-a8b5c975><!--[--><p class="text" data-v-983f6b21>拍照简易构图法</p><!--]--><!----></a><!----></div><!----></div><!--]--></div></section></div><!--]--><!--[--><!--]--></nav></aside><div class="VPContent has-sidebar" id="VPContent" data-v-93a960b4 data-v-0bd490fb><div class="VPDoc has-sidebar has-aside" data-v-0bd490fb data-v-c5936a1e><div class="container" data-v-c5936a1e><div class="aside" data-v-c5936a1e><div class="aside-curtain" data-v-c5936a1e></div><div class="aside-container" data-v-c5936a1e><div class="aside-content" data-v-c5936a1e><div class="VPDocAside" data-v-c5936a1e data-v-cdc66372><!--[--><!--]--><!--[--><!--]--><div class="VPDocAsideOutline" data-v-cdc66372 data-v-5dd9d5f6><div class="content" data-v-5dd9d5f6><div class="outline-marker" data-v-5dd9d5f6></div><div class="outline-title" data-v-5dd9d5f6>On this page</div><nav aria-labelledby="doc-outline-aria-label" data-v-5dd9d5f6><span class="visually-hidden" id="doc-outline-aria-label" data-v-5dd9d5f6> Table of Contents for current page </span><ul class="root" data-v-5dd9d5f6 data-v-1188541a><!--[--><!--]--></ul></nav></div></div><!--[--><!--]--><div class="spacer" data-v-cdc66372></div><!--[--><!--]--><!----><!--[--><!--]--><!--[--><!--]--></div></div></div></div><div class="content" data-v-c5936a1e><div class="content-container" data-v-c5936a1e><!--[--><!--]--><main class="main" data-v-c5936a1e><div style="position:relative;" class="vp-doc _Blogs_MySqlStudy_MySQL" data-v-c5936a1e><div><h1 id="简述" tabindex="-1">简述 <a class="header-anchor" href="#简述" aria-hidden="true">#</a></h1><h2 id="文档" tabindex="-1">文档 <a class="header-anchor" href="#文档" aria-hidden="true">#</a></h2><ol><li><a href="https://docs.gitcode.net/mysql/guide/" target="_blank" rel="noreferrer">MySQL 中文文档 | MySQL 中文文档 (gitcode.net)</a><ul><li>最新文档</li></ul></li><li><a href="https://www.mysqlzh.com/" target="_blank" rel="noreferrer">MySQL 中文文档 | MySQL 中文网 (mysqlzh.com)</a><ul><li>5.0版本</li></ul></li></ol><h2 id="特点" tabindex="-1">特点 <a class="header-anchor" href="#特点" aria-hidden="true">#</a></h2><p style="color:green;">免费；开源；跨平台；真的好用！</p><h1 id="mysql的使用" tabindex="-1">MySQL的使用 <a class="header-anchor" href="#mysql的使用" aria-hidden="true">#</a></h1><h2 id="mysql的基本概念" tabindex="-1">MySQL的基本概念 <a class="header-anchor" href="#mysql的基本概念" aria-hidden="true">#</a></h2><p><strong>什么是MySQL？</strong><br>SQL全称是Structured Query Language：结构化查询语言<br>其实就是定义了操作所有关系型数据库的规则。</p><h2 id="安装" tabindex="-1">安装 <a class="header-anchor" href="#安装" aria-hidden="true">#</a></h2><p><strong>下载：</strong></p><p>去官网（www.mysql.com）→DOWNLOADS→MySQL Community (GPL) Downloads →<a href="https://dev.mysql.com/downloads/windows/" target="_blank" rel="noreferrer">MySQL Installer for Windows</a></p><p>**安装：**目录为C:\Program Files\MySQL\MySQL Server 8.0</p><p>**配置：**系统环境变量的PATH编辑添加安装的bin目录</p><h2 id="卸载" tabindex="-1">卸载 <a class="header-anchor" href="#卸载" aria-hidden="true">#</a></h2><ol><li><p>去C:\ProgramData\MySQL\MySQL Server 8.0目录下找到my.ini文件</p><p>，并复制datadir=C:/ProgramData/MySQL/MySQL Server 8.0\Data</p></li><li><p>写在MySQL</p></li><li><p>删除C:\ProgramData下的MySQL文件夹</p></li></ol><h1 id="mysql相关配置" tabindex="-1">MySQL相关配置 <a class="header-anchor" href="#mysql相关配置" aria-hidden="true">#</a></h1><h2 id="服务的启动" tabindex="-1">服务的启动 <a class="header-anchor" href="#服务的启动" aria-hidden="true">#</a></h2><p>1、手动。计算机管理→服务</p><p>2、cmd→services.msc 打开服务的窗口</p><p>3、使用管理员打开cmd</p><p>​ net start mysql：启动mysql的服务</p><p>​ net stop mysql：停止mysql的服务</p><h2 id="登录与退出" tabindex="-1">登录与退出 <a class="header-anchor" href="#登录与退出" aria-hidden="true">#</a></h2><p><strong>格式：</strong></p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">mysql -h主机名 -u用户名 -密码</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p><strong>各参数以及意义：</strong></p><ul><li><p>-h：表示启动服务器程序的计算机的域名或者IP地址，如果服务器程序在本机运行的话，可省略这个参数，也可填写localhost或者127.0.0.1；还可以写为“--host=主机”的形式</p></li><li><p>-u：表示用户名，超级管理员的用户名是root；也可以是“--user=用户名”的形式</p></li><li><p>-p：表示密码；也可以是“--password=密码”的形式</p></li></ul><p><strong>退出</strong>：quit、exit、\q</p><p><strong>连接注意事项：</strong></p><ul><li><u>最好不要在一行命令中输入密码；</u>此时输入密码会被任何程序监视到，不安全。 <br>可以先不为 -p参数指定值，如：<code>mysql -hlocalhost -uroot -p</code> 单击回车后会提示输入密码，此时输入时安全状态</li><li><u>如果非要显式地把密码写出来</u>，则密码值和 -p之间不能有空白字符，其他参数名之间可以有空白字符；<br>如：<code>mysql -h localhost -u root -p123456</code></li><li>mysql地各个参数地摆放顺序没有硬性规定</li><li>如果服务器和客户端程序安装在同一台计算器，-h参数可省略</li><li>如果使用的是类UNIX系统，并且省略了-u参数，则会把登录操作系统的用户名当作MySQL的用户名处理</li></ul><h1 id="mysql基础" tabindex="-1">MySQL基础 <a class="header-anchor" href="#mysql基础" aria-hidden="true">#</a></h1><h2 id="基础语法" tabindex="-1">基础语法 <a class="header-anchor" href="#基础语法" aria-hidden="true">#</a></h2><h3 id="通用语法" tabindex="-1">通用语法 <a class="header-anchor" href="#通用语法" aria-hidden="true">#</a></h3><ol><li>SQL语句可以单行或多行书写，以分号结尾。</li><li>可使用空格和缩进来增强语句的可读性。</li><li>MySQL数据库的SQL语句不区分大小写，关键字建议使用大写。</li><li>使用 \c 放弃本次操作</li><li>字符串的表示：用单引号(&#39;内容&#39;)或双引号(&quot;内容&quot;)把字符串内容引起来。</li><li>三种注释 <ol><li>单行注释：<code>-- 内容</code> ←--和内容之间必须加空格</li><li>单行注释：<code>#内容（mysql特有）</code></li><li>多行注释：<code>/*内容*/</code></li></ol></li></ol><h4 id="注意事项" tabindex="-1">注意事项 <a class="header-anchor" href="#注意事项" aria-hidden="true">#</a></h4><ul><li>语句结束符号：（;）（\g）（\G） <ul><li>\G 比较特殊，它以垂直的形式将结果中的每个列都展示在单独的一行。</li></ul></li><li>语句可以随意地换行，即没有语句结束符，该语句就不会结束。</li><li>可以一次提交多条语句。</li></ul><h2 id="数据类型" tabindex="-1">数据类型 <a class="header-anchor" href="#数据类型" aria-hidden="true">#</a></h2><h3 id="整数类型" tabindex="-1">整数类型 <a class="header-anchor" href="#整数类型" aria-hidden="true">#</a></h3><table><thead><tr><th style="text-align:center;">类型</th><th style="text-align:center;">大小</th><th style="text-align:left;">无符号取值范围</th><th style="text-align:left;">有符号取值范围</th></tr></thead><tbody><tr><td style="text-align:center;">TINYINT</td><td style="text-align:center;">1字节</td><td style="text-align:left;">0~2⁸-1</td><td style="text-align:left;">-2⁷~2⁷-1</td></tr><tr><td style="text-align:center;">SMALLINT</td><td style="text-align:center;">2字节</td><td style="text-align:left;">0~2¹⁶-1</td><td style="text-align:left;">-2¹⁵~2¹⁵-1</td></tr><tr><td style="text-align:center;">MEDIUMINT</td><td style="text-align:center;">3字节</td><td style="text-align:left;">0~2²⁴-1</td><td style="text-align:left;">-2²³~2²³-1</td></tr><tr><td style="text-align:center;">INT或INTEGER</td><td style="text-align:center;">4字节</td><td style="text-align:left;">0~2³²-1</td><td style="text-align:left;">-2³¹~2³¹-1</td></tr><tr><td style="text-align:center;">BEGINT</td><td style="text-align:center;">8字节</td><td style="text-align:left;">0~2⁶⁴-1</td><td style="text-align:left;">-2⁶³~2⁶³-1</td></tr></tbody></table><ol><li>在数据类型后加上 UNSIGNED 单词，则表示该类型用于无符号数。</li><li>在数据类型后加上 SIGNED 单词，或者什么都不加，则表示该类型表示有符号数。</li></ol><h3 id="浮点数类型" tabindex="-1">浮点数类型 <a class="header-anchor" href="#浮点数类型" aria-hidden="true">#</a></h3><table><thead><tr><th style="text-align:center;">类型</th><th style="text-align:center;">大小</th><th style="text-align:center;">取值范围</th><th style="text-align:center;">取值范围</th><th style="text-align:center;">含义</th></tr></thead><tbody><tr><td style="text-align:center;">FLOAT</td><td style="text-align:center;">4字节</td><td style="text-align:center;"></td><td style="text-align:center;"></td><td style="text-align:center;">单精度浮点数</td></tr><tr><td style="text-align:center;">DOUBLE</td><td style="text-align:center;">8字节</td><td style="text-align:center;"></td><td style="text-align:center;"></td><td style="text-align:center;">双精度浮点数</td></tr></tbody></table><ol><li>使用浮点数表示小数是不够精确的。</li><li>可用FLOAT(M,D)或DOUBLE(M,D)来限制可以存储到本列的小数范围。（不推荐使用） <ol><li>M表示该小数最多包含的有效数字个数</li><li>D表示该小数保留小数点后十进制数字的个数</li></ol></li></ol><h3 id="定点数类型" tabindex="-1">定点数类型 <a class="header-anchor" href="#定点数类型" aria-hidden="true">#</a></h3><table><thead><tr><th style="text-align:center;">类型</th><th style="text-align:center;">大小</th><th style="text-align:center;">取值范围</th></tr></thead><tbody><tr><td style="text-align:center;">DECIMAL(M,D)</td><td style="text-align:center;">取决于M和D</td><td style="text-align:center;">取决于M和D</td></tr></tbody></table><h3 id="字符串类型" tabindex="-1">字符串类型 <a class="header-anchor" href="#字符串类型" aria-hidden="true">#</a></h3><table><thead><tr><th style="text-align:center;">类型</th><th style="text-align:center;">最大长度</th><th style="text-align:center;">存储空间要求</th><th style="text-align:center;">含义</th></tr></thead><tbody><tr><td style="text-align:center;">CHAR(M)</td><td style="text-align:center;">M个字符</td><td style="text-align:center;">M*W字节</td><td style="text-align:center;">定长字符串</td></tr><tr><td style="text-align:center;">VARCHAR(M)</td><td style="text-align:center;">M个字符</td><td style="text-align:center;">L+1或L+2字节</td><td style="text-align:center;">变长字符串</td></tr><tr><td style="text-align:center;">TINYTEXT</td><td style="text-align:center;">2⁸-1字节</td><td style="text-align:center;">L+1字节</td><td style="text-align:center;"></td></tr><tr><td style="text-align:center;">TEXT</td><td style="text-align:center;">2¹⁶-1字节</td><td style="text-align:center;">L+2字节</td><td style="text-align:center;"></td></tr><tr><td style="text-align:center;">MEDIUMTEXT</td><td style="text-align:center;">2²⁴-1字节</td><td style="text-align:center;">L+3字节</td><td style="text-align:center;"></td></tr><tr><td style="text-align:center;">LONGTEXT</td><td style="text-align:center;">2³²-1字节</td><td style="text-align:center;">L+4字节</td><td style="text-align:center;"></td></tr></tbody></table><ol><li>M 代表该数据类型最多能存储的<u>字符</u>数量</li><li>W 代表在特定字符集下编码一个字符最多需要的字节数</li><li>L 代表实际想该类型的列中存储的字符串在特定字符集下所占的字节数</li></ol><h4 id="char-m" tabindex="-1">CHAR(M) <a class="header-anchor" href="#char-m" aria-hidden="true">#</a></h4><ul><li>CHAR(M)中的M代表该类型最多可以存储的字符数量。 <ul><li>M 的取值范围是（0~255）</li><li>若省略则默认为1</li><li>CHAR(0)，只能存储空字符串或NULL值</li></ul></li><li>CHAR(M)在不同的字符集下需要的存储空间也不一样 <ul><li>假设某个字符集编码一个字符最对需要W字节，则CHAR(M)占用的存储空间是M*W字节</li><li>ASCII：W=1</li><li>GBK：W=2</li><li>UTF-8：W=3</li></ul></li><li>如果实际存储的字符串在特定字符集编码下占用的字节数不足M*W，那么剩余的存储空间用空格字符补齐。</li></ul><h4 id="varchar-m" tabindex="-1">VARCHAR(M) <a class="header-anchor" href="#varchar-m" aria-hidden="true">#</a></h4><ul><li><p>VARCHAR(M)对应的存储空间由下面两部分组成</p><ol><li><p>真正的字符串内容</p><ol><li>假设真正的字符串在采用特定字符集编码后占用的字节数为L</li></ol></li><li><p>占用的字节数</p><p>假设VARCHAR(M)类型采用的字符集编码一个字符最多需要W字节，那么</p><ol><li>当W*M &lt; 256时，所需存储空间为 L+1</li><li>当W*M &gt;= 256时，所需存储空间为 L+2</li></ol></li></ol></li><li><p>VARCHAR(M)的性能低于CHAR(M)</p></li></ul><h4 id="text" tabindex="-1">TEXT <a class="header-anchor" href="#text" aria-hidden="true">#</a></h4><p>表中如果有的属性需要存储特别长的文本，可考虑使用这几个类型</p><h4 id="enum和set" tabindex="-1">ENUM和SET <a class="header-anchor" href="#enum和set" aria-hidden="true">#</a></h4><ul><li>ENUM类型，也就是枚举类型 <ol><li>格式：ENUM(&#39;str1&#39;,&#39;str2&#39;,&#39;str3&#39;,...)</li><li>对于使用ENUM类型的列，该列的值只能在给定的字符串中选取其中一个</li></ol></li><li>SET类型 <ol><li>格式：SET(&#39;str1&#39;,&#39;str2&#39;,&#39;str3&#39;,...)</li><li>对于使用SET类型的列，该列的值可以在给定的字符串列表中选择一个或多个</li></ol></li></ul><h4 id="二进制类型" tabindex="-1">二进制类型 <a class="header-anchor" href="#二进制类型" aria-hidden="true">#</a></h4><table><thead><tr><th style="text-align:center;">类型</th><th style="text-align:center;">占用的存储空间（单位：字节）</th><th style="text-align:center;">含义</th></tr></thead><tbody><tr><td style="text-align:center;">BIT(M)</td><td style="text-align:center;">近似为(M+7)/8</td><td style="text-align:center;">存储M个二进制位的值</td></tr><tr><td style="text-align:center;">BINARY(M)</td><td style="text-align:center;"></td><td style="text-align:center;">定长</td></tr><tr><td style="text-align:center;">VARBINARY(M)</td><td style="text-align:center;"></td><td style="text-align:center;">变长</td></tr><tr><td style="text-align:center;">TINYBLOB</td><td style="text-align:center;">2⁸-1</td><td style="text-align:center;"></td></tr><tr><td style="text-align:center;">BLOB</td><td style="text-align:center;">2¹⁶-1</td><td style="text-align:center;"></td></tr><tr><td style="text-align:center;">MEDIUMBLOB</td><td style="text-align:center;">2²⁴-1</td><td style="text-align:center;"></td></tr><tr><td style="text-align:center;">LONGBLOB</td><td style="text-align:center;">2³²-1</td><td style="text-align:center;"></td></tr></tbody></table><h5 id="bit" tabindex="-1">BIT <a class="header-anchor" href="#bit" aria-hidden="true">#</a></h5><ul><li>M的取值范围(1~64)，且M可省略，默认为1</li><li>若存储的比特数不足以整个字节，则按一个字节计算</li></ul><h5 id="binary-m-和varbinary-m" tabindex="-1">BINARY(M)和VARBINARY(M) <a class="header-anchor" href="#binary-m-和varbinary-m" aria-hidden="true">#</a></h5><ul><li>与CHAR(M)和VARCHAR(M)类似</li></ul><h5 id="blob" tabindex="-1">BLOB <a class="header-anchor" href="#blob" aria-hidden="true">#</a></h5><ul><li>用于存储可变长度的二进制数据，比如图片、音频、压缩文件等。</li><li>对于比较大的二进制数据，通常不直接存储到数据库管理系统中，而是保存在文件系统中，在数据库中存储文件路径。</li></ul><h4 id="日期和时间类型" tabindex="-1">日期和时间类型 <a class="header-anchor" href="#日期和时间类型" aria-hidden="true">#</a></h4><table><thead><tr><th style="text-align:center;">类型</th><th style="text-align:center;">大小</th><th style="text-align:center;">范围</th><th style="text-align:center;">含义</th></tr></thead><tbody><tr><td style="text-align:center;">YEAR</td><td style="text-align:center;">1字节</td><td style="text-align:center;">1901至2155</td><td style="text-align:center;">年份值</td></tr><tr><td style="text-align:center;">DATE</td><td style="text-align:center;">3字节</td><td style="text-align:center;">1000-01-01至9999-12-31</td><td style="text-align:center;">日期值</td></tr><tr><td style="text-align:center;">TIME</td><td style="text-align:center;">3字节+小数秒的大小</td><td style="text-align:center;">-838:59:59[.000000]至838.59.59[.000000]</td><td style="text-align:center;">时间值</td></tr><tr><td style="text-align:center;">DATETIME</td><td style="text-align:center;">5字节+小数秒的大小</td><td style="text-align:center;">1000-01-01 00:00:00[.000000]至9999-12-31 23:59:59[.999999]</td><td style="text-align:center;">日期加时间值</td></tr><tr><td style="text-align:center;">TIMESTAMP</td><td style="text-align:center;">4字节+小数秒的大小</td><td style="text-align:center;">1970-01-01 00:00:01[.000000]至2038-01-19 03:24:07[.999999]</td><td style="text-align:center;">时间戳</td></tr></tbody></table><p>让TIME、DATETIME、TIMESTAMP支持小数秒的语法：<code>数据类型(小数秒位数)</code></p><table><thead><tr><th style="text-align:center;">保留的小数秒位数</th><th style="text-align:center;">额外需要的存储空间</th></tr></thead><tbody><tr><td style="text-align:center;">0</td><td style="text-align:center;">0字节</td></tr><tr><td style="text-align:center;">1或2</td><td style="text-align:center;">1字节</td></tr><tr><td style="text-align:center;">3或4</td><td style="text-align:center;">2字节</td></tr><tr><td style="text-align:center;">5或6</td><td style="text-align:center;">3字节</td></tr></tbody></table><h5 id="year" tabindex="-1">YEAR <a class="header-anchor" href="#year" aria-hidden="true">#</a></h5><ul><li>取值范围是1901~2155</li><li>如果想存储更大范围的年份值，可使用SMALLINT（2字节）或者字符串类型</li></ul><h5 id="date" tabindex="-1">DATE <a class="header-anchor" href="#date" aria-hidden="true">#</a></h5><ul><li>表示日期，格式是YYYY-MM-DD</li></ul><h5 id="time" tabindex="-1">TIME <a class="header-anchor" href="#time" aria-hidden="true">#</a></h5><ul><li>表示时间，格式是hh:mm:ss[.uuuuuu] 或者 hhh:mm:ss[.uuuuuu]</li><li>TIME可表示一天中的某个时间，也可表示某一段时间（某两个时间的时间间隔）</li><li>这导致TIME可能表示的小时数比较大，并且可能为负值。</li></ul><h5 id="datetime" tabindex="-1">DATETIME <a class="header-anchor" href="#datetime" aria-hidden="true">#</a></h5><ul><li>表示日期和时间，格式是YYYY-MM-DD hh:mm:ss[.uuuuuu]</li><li>DATETIME中存储的时间必须是一天内的某个时间（也就是小时数必须小于24）</li></ul><h5 id="timestamp" tabindex="-1">TIMESTAMP <a class="header-anchor" href="#timestamp" aria-hidden="true">#</a></h5><ul><li>TIMESTAMP存储的时间所展示的值随着时区的变化而变化</li></ul><h2 id="文档-1" tabindex="-1">文档 <a class="header-anchor" href="#文档-1" aria-hidden="true">#</a></h2><ol><li><a href="https://docs.gitcode.net/mysql/guide/" target="_blank" rel="noreferrer">MySQL 中文文档 | MySQL 中文文档 (gitcode.net)</a><ul><li>最新文档</li></ul></li><li><a href="https://www.mysqlzh.com/" target="_blank" rel="noreferrer">MySQL 中文文档 | MySQL 中文网 (mysqlzh.com)</a><ul><li>5.0版本</li></ul></li></ol><h2 id="mysql的分类" tabindex="-1">MySQL的分类 <a class="header-anchor" href="#mysql的分类" aria-hidden="true">#</a></h2><ul><li><p>DDL（Data Definition Lauguage）数据库定义语言</p><ul><li>用来定义数据库对象：数据库，表，列表，列等。</li><li>关键字：create，drop，alter等</li></ul></li><li><p>DML（Data Manipulation Language）数据操作语言</p><ul><li>用来对数据库中表的数据进行增删改查。</li><li>关键字：insert，delete，update等</li></ul></li><li><p>DQL（Data Query Language）数据查询语言</p><ul><li>用来查询数据库中表的记录（数据）</li><li>关键字：select，where等</li></ul></li><li><p>DCL（Data Control Language）数据库控制语言</p><ul><li>用来定义数据库的访问权限和安全级别，及创建用户。</li><li>关键字：GRANT，REVOKE等</li></ul></li></ul><h1 id="ddl-操作数据库和表" tabindex="-1">DDL：操作数据库和表 <a class="header-anchor" href="#ddl-操作数据库和表" aria-hidden="true">#</a></h1><h2 id="操作数据库-crud" tabindex="-1">操作数据库：CRUD <a class="header-anchor" href="#操作数据库-crud" aria-hidden="true">#</a></h2><ol><li>C（Create）：创建</li><li>R（Retrieve）：查询</li><li>U（Update）：修改</li><li>D(Delete)：删除</li></ol><h3 id="查询" tabindex="-1">查询 <a class="header-anchor" href="#查询" aria-hidden="true">#</a></h3><ul><li><p>查询所有数据库：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW DATABASES;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li><li><p>查询当前数据库：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT DATABASE();</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ul><h3 id="创建" tabindex="-1">创建 <a class="header-anchor" href="#创建" aria-hidden="true">#</a></h3><ul><li><p>创建数据库：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE DATABASE 数据库名;</span></span>
<span class="line"><span style="color:#A6ACCD;">或者</span></span>
<span class="line"><span style="color:#A6ACCD;">CTEATE DATABASE [IF NOT EXISTS] 数据库名 [DEFAULT CHARSET 字符集] [COLLATE 排序规则]</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div></li><li><p>IF NOT EXISTS：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE DATABASE IF NOT EXISTS 数据库名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>这条语句的意思是，如果指定的数据库不存在就创建，若存在就什么也不做，只提出warning</li><li>而用第一种方法创建数据库，若已存在，会报错（ERROR）</li></ul></li></ul><h3 id="删除" tabindex="-1">删除 <a class="header-anchor" href="#删除" aria-hidden="true">#</a></h3><ul><li><p>删除数据库：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DROP DATABASE 数据库名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li><li><p>IF EXISTS：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DROP DARABASE IF EXISTS 数据库名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>这条语言和前面提到的类似，在此就不做赘述。</li></ul></li></ul><h3 id="使用" tabindex="-1">使用 <a class="header-anchor" href="#使用" aria-hidden="true">#</a></h3><ul><li><p>切换数据库：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">USE 数据库名</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>切换当前使用的数据库，结尾分号可加可不加。</li></ul></li><li><p>连接时选择数据库：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">mysql -h localhost -u root -p123456 数据库名</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>这样，当成功连接服务器后，“数据库名”就是当前客户端的默认数据库。</li></ul></li></ul><h2 id="表的基本操作" tabindex="-1">表的基本操作 <a class="header-anchor" href="#表的基本操作" aria-hidden="true">#</a></h2><h3 id="查询-1" tabindex="-1">查询 <a class="header-anchor" href="#查询-1" aria-hidden="true">#</a></h3><ul><li><p>查询表：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW TABLES [FROM 数据库名];</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>默认查询正在使用的表。</li><li>若查询的数据库没表，会返回 Empty set（空集）</li></ul></li><li><p>查询表结构：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DESCRIBE 表名;  DESC 表名;  EXPLAIN 表名;  SHOW FIELDS FROM 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;">SHOW COLUMNS FROM 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>以上代码效果一样，都可以查询表结构。</p></li><li><p>查询指定表的建表语句</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW CREATE TABLE 表名\G</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>此语句的输出是创建表时填写的语句，且自动添加属性。</p></li></ul><h3 id="创建-1" tabindex="-1">创建 <a class="header-anchor" href="#创建-1" aria-hidden="true">#</a></h3><ul><li><p>创建表：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE TABLE 表名(</span></span>
<span class="line"><span style="color:#A6ACCD;">	列名1 数据类型 [列的属性],</span></span>
<span class="line"><span style="color:#A6ACCD;">    列名2 数据类型 [列的属性],</span></span>
<span class="line"><span style="color:#A6ACCD;">    ...</span></span>
<span class="line"><span style="color:#A6ACCD;">    列名n 数据类型 [列的属性]</span></span>
<span class="line"><span style="color:#A6ACCD;">)[COMMENT `表的注释信息`];</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>创建表的语句可在单行，多行只为美观。</p><p>最后一个语句没有逗号结尾</p><p>COMMENT：为建表语句添加注释</p></li><li><p>IF NOT EXISTS：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE TABLE IF NOT EXISTS 表名(</span></span>
<span class="line"><span style="color:#A6ACCD;">	各个列的信息 ...</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>与数据库的 IF NOT EXISTS 类似，在此不多做赘述。</p></li></ul><h3 id="删除-1" tabindex="-1">删除 <a class="header-anchor" href="#删除-1" aria-hidden="true">#</a></h3><ul><li><p>删除表</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DROP TABLE 表1,表2,...,表n;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 删除指定表，并重新创建该表</span></span>
<span class="line"><span style="color:#A6ACCD;">TRUNCATE TABLE 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div></li><li><p>IF ESISTS</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DROP TABLE IF ExISTS 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>与数据库的 IF EXISTS 类似，在此不多做赘述。</p></li></ul><h3 id="使用-1" tabindex="-1">使用 <a class="header-anchor" href="#使用-1" aria-hidden="true">#</a></h3><ul><li>直接使用某个数据库中的某个表 <ul><li>数据库.表名</li></ul></li></ul><h3 id="修改" tabindex="-1">修改 <a class="header-anchor" href="#修改" aria-hidden="true">#</a></h3><h4 id="修改表名" tabindex="-1">修改表名 <a class="header-anchor" href="#修改表名" aria-hidden="true">#</a></h4><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 方式一</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE 旧表名 RENAME TO 新表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 方式二</span></span>
<span class="line"><span style="color:#A6ACCD;">RENAME TABLE 旧表名1 TO 新表名1,旧表名2 TO 新表名2,...旧表名n TO 新表名n;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li>如果在修改表名的时候指定了数据库名，还可以将该表转移到对应的数据库下。</li></ul><h4 id="增加列" tabindex="-1">增加列 <a class="header-anchor" href="#增加列" aria-hidden="true">#</a></h4><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 ADD COLUMN 列名 数据类型 [列的属性];</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 添加到第一列</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 ADD COLUMN 列名 数据类型 [列的属性] FIRST;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 添加到指定列的后面</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 ADD COLUMN 列名 数据类型 [列的属性] AFTER 指定列名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="修改列信息" tabindex="-1">修改列信息 <a class="header-anchor" href="#修改列信息" aria-hidden="true">#</a></h4><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 修改列的数据类型和属性</span></span>
<span class="line"><span style="color:#A6ACCD;">	-- 方式一</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 MODIFY 列名 新数据类型 [新属性];</span></span>
<span class="line"><span style="color:#A6ACCD;">	-- 方式二，也可修改列名</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 CHANGE 旧列名 新列名 新数据类型 [新属性];</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 将某列设置为表的第一列</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 MODIFY 列名 列的类型 列的属性 FIRST;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 将列放在指定列的后面</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 MODIFY 列名 列的类型 列的属性 AFTER 指定列名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 若同一表中有多列修改操作，可放在一条语句</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 操作1,操作2,...,操作n;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h4 id="删除列" tabindex="-1">删除列 <a class="header-anchor" href="#删除列" aria-hidden="true">#</a></h4><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 删除表中的列</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 DROP COLUMN 列名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h1 id="列的属性" tabindex="-1">列的属性 <a class="header-anchor" href="#列的属性" aria-hidden="true">#</a></h1><h2 id="简单的查询和插入" tabindex="-1">简单的查询和插入 <a class="header-anchor" href="#简单的查询和插入" aria-hidden="true">#</a></h2><h3 id="简单的查询语句" tabindex="-1">简单的查询语句 <a class="header-anchor" href="#简单的查询语句" aria-hidden="true">#</a></h3><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询某个表中已经存储了哪些数据</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="简单的插入语句" tabindex="-1">简单的插入语句 <a class="header-anchor" href="#简单的插入语句" aria-hidden="true">#</a></h3><ul><li>在mysql中插入数据的时候是以行为单位的，一行数据也成为一条记录。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">INSERT INTO 表名 VALUES(列1的值,列2的值,...,列n的值);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 批量插入</span></span>
<span class="line"><span style="color:#A6ACCD;">INSERT INTO 表名(列1,列2,...) VALUES(列1的值,列2的值,...),(列1的值,列2的值,...),...;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li>也可以只指定部分的列，没有显式指定的列的值将被设置为NULL。</li></ul><h2 id="列的属性-1" tabindex="-1">列的属性 <a class="header-anchor" href="#列的属性-1" aria-hidden="true">#</a></h2><h3 id="默认值" tabindex="-1">默认值 <a class="header-anchor" href="#默认值" aria-hidden="true">#</a></h3><p>-- 列的默认值为NULL，若要修改列的默认值，可在定义列的时候给列增加一个DEFAULT属性。</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">列名 列的类型 DEFAULT 默认值</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">示例：</span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE TABLE first_table(</span></span>
<span class="line"><span style="color:#A6ACCD;">	first_column INT,</span></span>
<span class="line"><span style="color:#A6ACCD;">    second_column VARCHAR(100) DEFAULT &#39;abc&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 这样之后second_column的默认值就成为了abc</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="not-null" tabindex="-1">NOT NULL <a class="header-anchor" href="#not-null" aria-hidden="true">#</a></h3><p>-- 规定不能存放NULL，可给列添加NOT NULL属性，语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">列名 列的类型 NOT NULL</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">示例：</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE first_table MODIFY first_column INT NOT NULL;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>此后再在此列插入NULL，将会报错。</p><h3 id="主键" tabindex="-1">主键 <a class="header-anchor" href="#主键" aria-hidden="true">#</a></h3><ul><li><strong>候选键</strong>：有时候在表中可以通过某个列或某些列的列组合来确定一条唯一的记录，我们可以把这个列或者这些列的列组合称为候选键。</li><li>一个表中可能有多个候选键，可以选择一个候选键作为表的主键</li><li>**主键：**一个表中只能有一个主键，主键的值不能重复，可以通过主键找到唯一的一条记录。</li></ul><p>可以通过下面的方式定义主键：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 如果主键只是单个列的话，可直接在该列后面声明PRIMARY KEY。</span></span>
<span class="line"><span style="color:#A6ACCD;">示例：</span></span>
<span class="line"><span style="color:#A6ACCD;">把student_info表的“学号”列声明为主键</span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE TABLE student_info(</span></span>
<span class="line"><span style="color:#A6ACCD;">	number INT PRIMARY KEY,</span></span>
<span class="line"><span style="color:#A6ACCD;">    name VARCHAR(5),</span></span>
<span class="line"><span style="color:#A6ACCD;">    ...</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 也可以把主键的声明单独提取出来，用下面这样的形式声明：</span></span>
<span class="line"><span style="color:#A6ACCD;">PRIMARY KEY (列名1，列名2，...)</span></span>
<span class="line"><span style="color:#A6ACCD;">然后把主键声明放在列定义后</span></span>
<span class="line"><span style="color:#A6ACCD;">实例：</span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE TABLE student_info(</span></span>
<span class="line"><span style="color:#A6ACCD;">	number INT,</span></span>
<span class="line"><span style="color:#A6ACCD;">    name VARCHAR(5),</span></span>
<span class="line"><span style="color:#A6ACCD;">    ...,</span></span>
<span class="line"><span style="color:#A6ACCD;">    PRIMARY KEY(number)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 对于多个列的列组合作为主键的情况下，必须使用单独声明形式</span></span>
<span class="line"><span style="color:#A6ACCD;">示例：</span></span>
<span class="line"><span style="color:#A6ACCD;">比如student_score表中number和subject的列组合作为主键</span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE TABLE student_score(</span></span>
<span class="line"><span style="color:#A6ACCD;">	number INT,</span></span>
<span class="line"><span style="color:#A6ACCD;">    subject VARCHAR(30),</span></span>
<span class="line"><span style="color:#A6ACCD;">    score TINYINT,</span></span>
<span class="line"><span style="color:#A6ACCD;">    PRIMARY KEY (number,subject)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><p>-- 注意：主键列默认具有NOT NULL属性。</p><h3 id="unique约束" tabindex="-1">UNIQUE约束 <a class="header-anchor" href="#unique约束" aria-hidden="true">#</a></h3><ul><li>UNIQUE约束是列约束或表约束，它定义了将列或列组中的值约束为唯一的规则。</li><li>也就是值是唯一的，不可重复。</li></ul><p>与建表语句中声明主键的方式类似，声明UNIQUE约束也有两种</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 如果想为单个列声明UNIQUE约束，可直接在该列后填写UNIQUE或UNIQUE KEY。</span></span>
<span class="line"><span style="color:#A6ACCD;">示例：</span></span>
<span class="line"><span style="color:#A6ACCD;">把student_info表的id_number列添加UNIQUE约束</span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE TABLE student_info(</span></span>
<span class="line"><span style="color:#A6ACCD;">	number INT PRIMARY KEY,</span></span>
<span class="line"><span style="color:#A6ACCD;">    name VARCHAR(5),</span></span>
<span class="line"><span style="color:#A6ACCD;">    id_number CHAR(18) UNIQUE,</span></span>
<span class="line"><span style="color:#A6ACCD;">    ...</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 也可以把UNIQUE约束的声明单独提取出来，用下面这样的形式声明：</span></span>
<span class="line"><span style="color:#A6ACCD;">UNIQUE [KEY] [约束名称] (列名1,列名2,...)</span></span>
<span class="line"><span style="color:#A6ACCD;">示例：</span></span>
<span class="line"><span style="color:#A6ACCD;">对id_number列的UNIQUE约束起名为uk_id_number</span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE TABLE student_info(</span></span>
<span class="line"><span style="color:#A6ACCD;">	number INT PRIMARY KEY,</span></span>
<span class="line"><span style="color:#A6ACCD;">    name VARCHAR(5),</span></span>
<span class="line"><span style="color:#A6ACCD;">    id_number CHAR(18),</span></span>
<span class="line"><span style="color:#A6ACCD;">    ...,</span></span>
<span class="line"><span style="color:#A6ACCD;">    UNIQUE KEY uk_id_number (id_number)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 对于多个列的列组合添加UNIQUE约束，必须使用单独声明形式</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br></div></div><ul><li>对于<u>约束名称</u>：每个约束都有自己的名字。 <ul><li>主键约束的名字是PRIMARY，为MYSQL自动添加，用户不可修改</li><li>而对于UNIQUE约束，可自定义，也可默认。</li></ul></li></ul><p><strong>主键和UNIQUE约束对比：</strong></p><ul><li>两者都能保证某个列或列组合的唯一性</li><li>一张表中只能定义一个主键，却可以定义多个UNIQUE约束</li><li>主键列中不允许存放NULL，而声明了UNIQUE约束的列可以存放NULL，而且NULL可以重复的出现在多条记录中。</li><li>如果没有给表定义主键，MySQL会将第一个声明为NOT NULL并且具有UNIQUE约束的列或列组合自动定义为主键。</li></ul><h3 id="外键" tabindex="-1">外键 <a class="header-anchor" href="#外键" aria-hidden="true">#</a></h3><p><u>外键约束</u>是指用于在两个表之间建立关系，需要指定引用主表的哪一列。</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CONSTRAINT [外键名称] FOREIGN KEY(列1,列2,...) REFERENCES 父表名(父列1,父列2,...);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>外键名是可选的，仅作名字使用。</li><li>如果A表中的某个列或者某些列依赖于B表中的某个列或某些列，那么就称A表为子表，B表为父表。</li><li>子表和父表使用外键关联起来</li></ul><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 由上举例，student_score表的number列依赖于student_info的number列</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 所以student_info是父表，student_score是子表，如下定义：</span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE TABLE student_score(</span></span>
<span class="line"><span style="color:#A6ACCD;">	number INT,</span></span>
<span class="line"><span style="color:#A6ACCD;">    subject VARCHAR(30),</span></span>
<span class="line"><span style="color:#A6ACCD;">    score TINYINT,</span></span>
<span class="line"><span style="color:#A6ACCD;">    PRIMARY KEY (number,subject),</span></span>
<span class="line"><span style="color:#A6ACCD;">    CONSTRAINT FOREIGN KEY(number) REFERENCES student_info(number)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">这样之后，再向student_score表插入记录的时候，MySQL都会检查插入的学号是否能在student_info表中找到，找不到则会报错。</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p><u>在MySQL中，父表中被子表依赖的列或列组合必须建立索引。</u></p><h3 id="auto-increment" tabindex="-1">AUTO_INCREMENT <a class="header-anchor" href="#auto-increment" aria-hidden="true">#</a></h3><ul><li>自增</li><li>可以为使用整数类型或浮点数类型的列声明该属性，在之后插入新记录时，可以不显示指定该列的值，MySQL会自动帮该列生成自动增长的唯一值。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">列名 列的类型 AUTO_INCREMENT</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">示例：</span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE TABLE first_table(</span></span>
<span class="line"><span style="color:#A6ACCD;">	id INT UNSIGNED AUTO_INCREMENT PRIMARY KEY,</span></span>
<span class="line"><span style="color:#A6ACCD;">    first_column INT,</span></span>
<span class="line"><span style="color:#A6ACCD;">    second_column VARCHAR(100) DEFAULT &#39;abc&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;">将id添加AUTO_INCREMENT属性</span></span>
<span class="line"><span style="color:#A6ACCD;">当我们在插入新记录而忽略该列，该列的值会自增1</span></span>
<span class="line"><span style="color:#A6ACCD;">若显式声明该值，则以显式为准</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br></div></div><p>注意：</p><ol><li>一个表中最多有一个具有AUTO_INCREMENT属性的列</li><li>具有AUTO_INCREMENT属性的列必须建立索引。主键和具有UNIQUE属性的列会自动建立索引</li><li>拥有AUTO_INCREMENT属性的列不能再通过指定DEFAULT属性来指定默认值</li><li>AUTO_INCREMENT一般作为主键的属性，来自动生成唯一标识一条记录的主键值</li></ol><h3 id="列的注释" tabindex="-1">列的注释 <a class="header-anchor" href="#列的注释" aria-hidden="true">#</a></h3><p>-- 每个列的末尾都可以添加COMMENT语句来为列添加注释。</p><h3 id="显示宽度与zerofill" tabindex="-1">显示宽度与ZEROFILL <a class="header-anchor" href="#显示宽度与zerofill" aria-hidden="true">#</a></h3><p>​ <strong>ZEROFILL简介</strong></p><ul><li>对于<u>无符号整数类型</u>的列，若要在查询结果中让数字左边补0，可给该列加<u>ZEROFILL属性</u>；</li><li>加入该属性后，列的类型就变为INT(10)，其中“10”就是显示宽度。</li><li>可指定显示宽度。</li></ul><p>​ <strong>注意事项</strong></p><ul><li>在创建表时，若声明了ZEROFILL属性的列没有声明UNSIGNED属性，那么MySQL会为该列自动生成该属性。</li><li>不同的整数类型有不同的默认显示宽度。TINYINT默认为4，INT默认为11；若加了UNSIGNED属性，则该类型的显示宽度减1。</li><li>显示宽度并不会影响列的所需存储空间以及取值范围。</li><li>只有列的实际值的位数小于显示宽度时，才会补0。</li><li>只设置显示宽度，而不设置ZEROFILL属性，则队查询结果无影响。</li></ul><h3 id="标识符的命名" tabindex="-1">标识符的命名 <a class="header-anchor" href="#标识符的命名" aria-hidden="true">#</a></h3><p>​ <strong>MySQL不欢迎的命名方式</strong></p><ol><li>名称中全部是数字</li><li>名称中有空白字符</li><li>名称中使用了MySQL的保留字</li></ol><ul><li>倘若非要用，可用反引号(``)把定义的名称引起来，但也不支持空格结尾这种脑残行为</li></ul><h1 id="dml-操作表的数据" tabindex="-1">DML：操作表的数据 <a class="header-anchor" href="#dml-操作表的数据" aria-hidden="true">#</a></h1><h2 id="dml介绍" tabindex="-1">DML介绍 <a class="header-anchor" href="#dml介绍" aria-hidden="true">#</a></h2><ul><li>DML全称是Data Manipulation Language(数据操作语言)，用来对数据库中表的数据记录进行增删改查操作。</li><li>添加数据(INSERT)</li><li>修改数据(UPDATE)</li><li>删除数据(DELETE)</li></ul><h2 id="插入数据" tabindex="-1">插入数据 <a class="header-anchor" href="#插入数据" aria-hidden="true">#</a></h2><ul><li>在关系型数据库中，数据一般都是以记录（行）为单位插入表中的。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">INSERT INTO 表名 VALUES(列1的值,列2的值,...,列n的值);</span></span>
<span class="line"><span style="color:#A6ACCD;">-- VALUES子句中参数的顺序与表中各个列的顺序一一对应</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 在使用此语法（插入一条完整记录的语法）时，VALUES子句中必须给出表中所有列的值，若无则填NULL（前提是没有声明NOT NULL属性）。</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 也可以显式指定列的插入顺序</span></span>
<span class="line"><span style="color:#A6ACCD;">INSERT INTO 表名(列1名,列2名,...,列n名) VALUES(列1的值,列2的值,...,列n的值);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 批量添加数据</span></span>
<span class="line"><span style="color:#A6ACCD;">INSERT INTO 表名(列1名,列2名,...,列n名) VALUES(值1,值2,...),(值1,值2,...),(值1,值2,...),...;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><ul><li>可以将某个查询的结果插入到表中。</li></ul><h3 id="insert-ignore" tabindex="-1">INSERT IGNORE <a class="header-anchor" href="#insert-ignore" aria-hidden="true">#</a></h3><ul><li>对于是主键或UNIQUE键的列或列组合来说，若不存在与待插入的值相同的值，则执行，否则不执行（而不是报错）。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- INSERT IGNORE语法</span></span>
<span class="line"><span style="color:#A6ACCD;">INSERT IGNORE INTO first_table(first_column,second_column) VALUES(1,&#39;值&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 执行结果：Query OK,0 rows affected,1 warning(0.00 sec)</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 这样在INSERT后加入IGNORE就不会报错，批量同样适用。</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="insert-on-duplicate-key-update" tabindex="-1">INSERT ... ON DUPLICATE KEY UPDATE <a class="header-anchor" href="#insert-on-duplicate-key-update" aria-hidden="true">#</a></h3><ul><li>与INSERT IGNORE类似，但相同后会执行额外语句。如下用法</li></ul><ol><li>现有一个first_table表的first_column列拥有UNIQUE约束，如果表中不包含与待插入记录的first_column列重复的记录，那么就把这条记录插入，否则将已存在的重复记录的first_column列更新为10，second_column列更新为&#39;雪碧&#39;。</li></ol><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">INSERT INTO first_table(first_column,second_column) VALUES(1,&#39;哇哈哈&#39;) ON DUPLICATE KEY UPDATE first_column = 10,second_column = &#39;雪碧&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ol><li>对于作为主键或UNIQUE键的列或者列组合来说，如果表中现有的记录在这些列或列组合上与待插入记录有重复的值，则可以使用VALUES(列名)的形式来引用待插入记录中对应列的值</li></ol><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">INSERT INTO first_table(first_column,second_column) VALUES(10,&#39;哇哈哈&#39;) ON DUPLICATE KEY UPDATE second_column = VALUES(second_column);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="删除数据" tabindex="-1">删除数据 <a class="header-anchor" href="#删除数据" aria-hidden="true">#</a></h2><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DELETE FROM 表名 [WHERE 表达式];</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 可以使用LIMIT子句限制删除的记录数量，使用ORDER BY子句来指定删除顺序，如：</span></span>
<span class="line"><span style="color:#A6ACCD;">DELETE FROM first_table ORDER BY first_column DESC LIMIT 1;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h2 id="更新数据" tabindex="-1">更新数据 <a class="header-anchor" href="#更新数据" aria-hidden="true">#</a></h2><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">UPDATE 表名 SET 列1=值1,列2=值2,...,列n=值n [WHERE 表达式];</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 可以使用LIMIT子句限制更新的记录数量，使用ORDER BY子句来指定更新顺序，如：</span></span>
<span class="line"><span style="color:#A6ACCD;">UPDATE first_table SET second_column=&#39;爽歪歪&#39; ORDER BY first_column DESC LIMIT 1;</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 修改id为1的数据，将name改为李三</span></span>
<span class="line"><span style="color:#A6ACCD;">UPDATE tb_user SET name=&#39;李三&#39; WHERE id=1;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>判断一个表达式的值是否为空，要用IS NULL，而非=</li></ul><h1 id="dql-查询表的数据" tabindex="-1">DQL：查询表的数据 <a class="header-anchor" href="#dql-查询表的数据" aria-hidden="true">#</a></h1><h2 id="dql介绍" tabindex="-1">DQL介绍 <a class="header-anchor" href="#dql介绍" aria-hidden="true">#</a></h2><p>DQL英文全称是Data Query Language(数据库查询语言)，数据查询语言，用来查询数据库中表的记录。</p><p>语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT -- &gt;字段列表</span></span>
<span class="line"><span style="color:#A6ACCD;">FROM -- &gt;表名列表</span></span>
<span class="line"><span style="color:#A6ACCD;">WHERE -- &gt;条件列表</span></span>
<span class="line"><span style="color:#A6ACCD;">GROUP BY -- &gt;分组字段列表</span></span>
<span class="line"><span style="color:#A6ACCD;">HAVING -- &gt;分组后条件列表</span></span>
<span class="line"><span style="color:#A6ACCD;">ORDER BY -- &gt;排序字段列表</span></span>
<span class="line"><span style="color:#A6ACCD;">LIMIT -- &gt;分页参数</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h3 id="子句编写顺序" tabindex="-1">子句编写顺序 <a class="header-anchor" href="#子句编写顺序" aria-hidden="true">#</a></h3><ul><li>如果在一个查询语句中出现了多个子句，那么它们之间的顺序是不能乱放的。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 顺序如下所示</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT [DISTINCT] 查询列表</span></span>
<span class="line"><span style="color:#A6ACCD;">[FROM 表名]</span></span>
<span class="line"><span style="color:#A6ACCD;">[WHERE 布尔表达式]</span></span>
<span class="line"><span style="color:#A6ACCD;">[GROUP BY 分组列表]</span></span>
<span class="line"><span style="color:#A6ACCD;">[HAVING 分组过滤条件]</span></span>
<span class="line"><span style="color:#A6ACCD;">[ORDER BY 排序列表]</span></span>
<span class="line"><span style="color:#A6ACCD;">[LIMIT 偏移量,限制条数]</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="子句执行顺序" tabindex="-1">子句执行顺序 <a class="header-anchor" href="#子句执行顺序" aria-hidden="true">#</a></h3><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">[FROM 表名]</span></span>
<span class="line"><span style="color:#A6ACCD;">     ↓↓</span></span>
<span class="line"><span style="color:#A6ACCD;">[WHERE 布尔表达式]</span></span>
<span class="line"><span style="color:#A6ACCD;">     ↓↓</span></span>
<span class="line"><span style="color:#A6ACCD;">[GROUP BY 分组列表]</span></span>
<span class="line"><span style="color:#A6ACCD;">     ↓↓</span></span>
<span class="line"><span style="color:#A6ACCD;">[HAVING 分组过滤条件]</span></span>
<span class="line"><span style="color:#A6ACCD;">     ↓↓</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT [DISTINCT] 查询列表</span></span>
<span class="line"><span style="color:#A6ACCD;">     ↓↓</span></span>
<span class="line"><span style="color:#A6ACCD;">[ORDER BY 排序列表]</span></span>
<span class="line"><span style="color:#A6ACCD;">     ↓↓</span></span>
<span class="line"><span style="color:#A6ACCD;">[LIMIT 偏移量,限制条数]</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><h2 id="简单查询" tabindex="-1">简单查询 <a class="header-anchor" href="#简单查询" aria-hidden="true">#</a></h2><h3 id="查询单列-列的别名" tabindex="-1">查询单列(列的别名) <a class="header-anchor" href="#查询单列-列的别名" aria-hidden="true">#</a></h3><p>查询某个表中某一列的数据的通用格式：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 列名 FROM 表名</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>SELECT子句：要查询的列名</li><li>FROM子句：要查询的表名</li></ul><p>也可以为结果集中的列重新定义一个别名</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 别名 [AS] 列的别名 FROM 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>AS可有可无</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT number AS 学号 FROM student_info;</span></span>
<span class="line"><span style="color:#A6ACCD;">与下相同</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number 学号 FROM student_info;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>注意：别名只是在<u>本次</u>查询到的结果集中展示，并不会改变真实表的列名。</p><h3 id="查询多列" tabindex="-1">查询多列 <a class="header-anchor" href="#查询多列" aria-hidden="true">#</a></h3><p>查询多列，需在SELECT子句写上多个列名</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 列名1,列名2,...列名n FROM 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 带上别名</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT 列名1 [AS] 列的别名,列名2 [AS] 列的别名,...列名n [AS] 列的别名 FROM 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="查询所有列" tabindex="-1">查询所有列 <a class="header-anchor" href="#查询所有列" aria-hidden="true">#</a></h3><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT * FROM 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="查询结果去重" tabindex="-1">查询结果去重 <a class="header-anchor" href="#查询结果去重" aria-hidden="true">#</a></h3><ul><li><p>单列去重</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT DISTINCT 列名 FROM 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li><li><p>多列去重</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT DISTINCT 列名1,列名2,...列名n FROM 表名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li></ul><h3 id="limit" tabindex="-1">LIMIT <a class="header-anchor" href="#limit" aria-hidden="true">#</a></h3><p>当结果集过多时，可使用LIMIT子句限制记录条数</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">LIMIT 限制条数</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">如：</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number FROM student_info LIMIT 2;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>默认从第1条记录开始</p><p>若不向从第一条记录开始，可用</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">LIMIT 偏移量，限制条数</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 偏移量：第n条记录的偏移量就是n-1</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">如：</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 从第3条记录开始，共取2条记录</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number FROM student_info LIMIT 2,2;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>​ -- 若偏移量大于或等于结果集的行数，则查询结果是空集。</p><h3 id="分页查询" tabindex="-1">分页查询 <a class="header-anchor" href="#分页查询" aria-hidden="true">#</a></h3><ul><li>语法</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 字段列表 FROM 表名 LIMIT 起始索引,查询记录数;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>注意 <ol><li>起始索引从0开始，起始索引=（查询页码-1）*每页显式记录数。</li><li>分页查询是数据库的方言，不同的数据库有不同的实现，MySQL中是LIMIT。</li><li>若查询的是第一页的数据，起始索引可省略。</li></ol></li></ul><h3 id="order-by-排序查询" tabindex="-1">ORDER BY(排序查询) <a class="header-anchor" href="#order-by-排序查询" aria-hidden="true">#</a></h3><p>可用ORDER BY子句显式指定排序规则</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 单列排序</span></span>
<span class="line"><span style="color:#A6ACCD;">ORDER BY 列名 [ASC|DESC]</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 多列排序</span></span>
<span class="line"><span style="color:#A6ACCD;">ORDER BY 列1 [ASC|DESC],列2 [ASC|DESC] ...</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">示例：</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 单列升序</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM student_score ORDER BY score;</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM student_score ORDER BY score ASC;</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 多列升序</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM student_score ORDER BY subject,score ASC;</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 结合ORDER BY子句和LIMIT子句</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM student_score ORDER BY score LIMIT 1;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><ul><li><p>ASC和DESC指的是排序方向</p><ul><li>ASC是按照指定列由小到大，即升序排列。</li><li>DESC是按照制定列由大到小，即降序排列。</li><li>默认是ASC。</li></ul></li><li><p>比较字符串的大小就是依次比较每个字符的大小。</p></li></ul><h3 id="查询案例" tabindex="-1">查询案例 <a class="header-anchor" href="#查询案例" aria-hidden="true">#</a></h3><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 1、查询年龄为20，21岁的人员信息</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM tb_user WHERE age in(20,21);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 2、查询性别为男，且年龄为20岁的姓名为两个字的人员信息</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM tb_user WHERE gender=&#39;男&#39; AND age=20 AND name LIKE &#39;__&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 3、统计年龄小于20岁的男性员工的人数</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM tb_user WHERE age&lt;20 AND gender=&#39;男&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 4、查询所有年龄小于等于20岁人员信息，并对查询结果按年龄升序排序，若年龄相同按id升序排序</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM tb_user WHERE age&lt;=20 ORDER BY age ASC,id DESC;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 5、查询性别为男，且年龄为20岁的前3个人员信息，并对查询结果按年龄升序排序，若年龄相同按id升序排序</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM tb_user WHERE age=20 ORDER BY age ASC,id ASC LIMIT 3;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h2 id="条件查询" tabindex="-1">条件查询 <a class="header-anchor" href="#条件查询" aria-hidden="true">#</a></h2><p>语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 列名 FROM 表名 WHERE 条件;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="运算符简述" tabindex="-1">运算符简述 <a class="header-anchor" href="#运算符简述" aria-hidden="true">#</a></h3><ul><li><strong>各种比较运算符</strong></li></ul><table><thead><tr><th style="text-align:center;">运算符</th><th style="text-align:center;">示例</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">=</td><td style="text-align:center;">a = b</td><td style="text-align:center;">a等于b</td></tr><tr><td style="text-align:center;">&lt;=&gt;</td><td style="text-align:center;">a &lt;=&gt; b</td><td style="text-align:center;">a等于b（&lt;=&gt; 称为NULL值安全等于运算符）</td></tr><tr><td style="text-align:center;">&lt;&gt;或!=</td><td style="text-align:center;">a &lt;&gt; b</td><td style="text-align:center;">a不等于b</td></tr><tr><td style="text-align:center;">&lt;</td><td style="text-align:center;">a &lt; b</td><td style="text-align:center;">a小于b</td></tr><tr><td style="text-align:center;">&lt;=</td><td style="text-align:center;">a &lt;= b</td><td style="text-align:center;">a小于或等于b</td></tr><tr><td style="text-align:center;">&gt;</td><td style="text-align:center;">a &gt; b</td><td style="text-align:center;">a大于b</td></tr><tr><td style="text-align:center;">&gt;=</td><td style="text-align:center;">a &gt;= b</td><td style="text-align:center;">a大于或等于b</td></tr><tr><td style="text-align:center;">BETWEEN</td><td style="text-align:center;">a BETWEEN b AND c</td><td style="text-align:center;">a的值必须满足 b&lt;=a&lt;=c</td></tr><tr><td style="text-align:center;">NOT BETWEEN</td><td style="text-align:center;">a NOT BETWEEN b AND c</td><td style="text-align:center;">a的值必须不满足b&lt;=a&lt;=c</td></tr></tbody></table><ul><li><strong>IN运算符</strong></li></ul><table><thead><tr><th style="text-align:center;">运算符</th><th style="text-align:center;">示例</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">IN</td><td style="text-align:center;">a IN (b1,b2,...)</td><td style="text-align:center;">a是b1,b2,...中的某一个</td></tr><tr><td style="text-align:center;">NOT IN</td><td style="text-align:center;">a NOT IN (b1,b2,...)</td><td style="text-align:center;">a不是b1,b2,...中的某一个</td></tr></tbody></table><ul><li><strong>NULL运算符</strong></li></ul><table><thead><tr><th style="text-align:center;">运算符</th><th style="text-align:center;">示例</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">IS NULL</td><td style="text-align:center;">a IS NULL</td><td style="text-align:center;">a的值是NULL</td></tr><tr><td style="text-align:center;">IS NOT NULL</td><td style="text-align:center;">a IS NOT NULL</td><td style="text-align:center;">a的值不是NULL</td></tr></tbody></table><ul><li><strong>多条件查询</strong></li></ul><table><thead><tr><th style="text-align:center;">运算符</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">AND或&amp;&amp;</td><td style="text-align:center;">符合所有搜索条件</td></tr><tr><td style="text-align:center;">OR或||</td><td style="text-align:center;">符合某一个搜索条件</td></tr></tbody></table><ul><li><strong>支持模糊查询的运算符</strong></li></ul><table><thead><tr><th style="text-align:center;">运算符</th><th style="text-align:center;">示例</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">LIKE</td><td style="text-align:center;">a LIKE b</td><td style="text-align:center;">a匹配b</td></tr><tr><td style="text-align:center;">NOT LIKE</td><td style="text-align:center;">a NOT LIKE b</td><td style="text-align:center;">a不匹配b</td></tr></tbody></table><ul><li><strong>算数运算符</strong></li></ul><table><thead><tr><th style="text-align:center;">运算符</th><th style="text-align:center;">示例</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">+</td><td style="text-align:center;">a + b</td><td style="text-align:center;">加法</td></tr><tr><td style="text-align:center;">-</td><td style="text-align:center;">a - b</td><td style="text-align:center;">减法</td></tr><tr><td style="text-align:center;">*</td><td style="text-align:center;">a * b</td><td style="text-align:center;">乘法</td></tr><tr><td style="text-align:center;">/</td><td style="text-align:center;">a / b</td><td style="text-align:center;">除法</td></tr><tr><td style="text-align:center;">DIV</td><td style="text-align:center;">a DIV b</td><td style="text-align:center;">除法，取商的整数部分</td></tr><tr><td style="text-align:center;">%</td><td style="text-align:center;">a % b</td><td style="text-align:center;">取余</td></tr><tr><td style="text-align:center;">-</td><td style="text-align:center;">-a</td><td style="text-align:center;">取负值</td></tr></tbody></table><ul><li><strong>逻辑运算符</strong></li></ul><table><thead><tr><th style="text-align:center;">运算符</th><th style="text-align:center;">示例</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">NOT（也可以写作 ! ）</td><td style="text-align:center;">NOT a</td><td style="text-align:center;">对a取反</td></tr><tr><td style="text-align:center;">AND（也可以写作 &amp;&amp; ）</td><td style="text-align:center;">a AND b</td><td style="text-align:center;">全真则真</td></tr><tr><td style="text-align:center;">OR（也可以写作 || ）</td><td style="text-align:center;">a OR b</td><td style="text-align:center;">全假则假</td></tr><tr><td style="text-align:center;">XOR</td><td style="text-align:center;">a XOR b</td><td style="text-align:center;">只有一个为真才为真</td></tr></tbody></table><h3 id="简单搜索条件" tabindex="-1">简单搜索条件 <a class="header-anchor" href="#简单搜索条件" aria-hidden="true">#</a></h3><p>使用方法</p><ul><li>把搜索条件放在WHERE子句中，再将WHERE子句放在FROM子句的后面</li></ul><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询student_info表中名字是“狗哥”的学生的一些信息</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number,name,id_number FROM student_info WHERE name = &#39;狗哥&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 查询学好在2~4的学生信息</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number,name,id_number FROM student_info WHERE number BETWEEN 2 AND 4;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="匹配列表中元素-in" tabindex="-1">匹配列表中元素(IN) <a class="header-anchor" href="#匹配列表中元素-in" aria-hidden="true">#</a></h3><p>作用：</p><ul><li>指定某一列的值是否在在个列表中</li></ul><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询“软件工程”和“会计”专业的学生信息</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number,name,id_number,major FROM student_info WHERE major IN (&#39;软件工程&#39;,&#39;会计&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="匹配null" tabindex="-1">匹配NULL <a class="header-anchor" href="#匹配null" aria-hidden="true">#</a></h3><p>作用：</p><ul><li>为某一列填入NULL意味着这一列的值尚未确定</li><li>判断某一列的值是否为NULL时，不能用=运算符</li><li>需要使用匹配NULL运算符</li></ul><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询student_info表中name列是NULL的学生记录有哪些</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number,name,id_number,major FROM student_info WHERE name IS NULL;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">若所有记录的name列的值都不是NULL，则返回Empty set</span></span>
<span class="line"><span style="color:#A6ACCD;">若有，则返回结果。</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>&lt;=&gt;：</p><ul><li>&lt;=&gt;运算符被称为NULL值安全等于运算符。</li><li>当&lt;=&gt;的操作数不含NULL时，其功能和 = 相同；</li><li>当&lt;=&gt;的一个操作数为NULL，另一个操作数不为NULL时，结果为0（FALSE）；</li><li>当两个操作数都为NULL，结果为1（TRUE）。</li></ul><h3 id="多条件查询-and-or" tabindex="-1">多条件查询(AND|OR) <a class="header-anchor" href="#多条件查询-and-or" aria-hidden="true">#</a></h3><p><strong>AND运算符</strong></p><p>作用：</p><ul><li>需要某条记录在符合所有搜索条件的时候才将其加入结果集</li></ul><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 从student_score表中找出科目为“语文”并且成绩大于75分的记录</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM student_score WHERE subject = &#39;语文&#39; AND score &gt;75;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>OR运算符</strong></p><p>作用：</p><ul><li>需要某条记录只需符合某一个搜索条件就将其加入结果集</li></ul><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 从student_score表中找出成绩大于95或者小于55分的记录</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM student_score WHERE score &gt; 95 OR score &lt; 55;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>更复杂的搜索条件的组合</strong></p><p>作用：</p><ul><li>在多条件的组合时，会因为运算符优先级判断失误而导致记录错误</li><li>故可使用小括号来显式的指定各个搜索条件的检测顺序</li></ul><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 从student_score表中找出课程为“语文”，并且成绩大于95或者小于55分的记录</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM student_score WHERE (score &gt; 95 OR score &lt; 55) AND subject = &#39;语文&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="通配符-like" tabindex="-1">通配符(LIKE) <a class="header-anchor" href="#通配符-like" aria-hidden="true">#</a></h3><p>作用：</p><ul><li>搜索条件是模糊的，称为模糊查询。</li><li>可使用通配符来代替模糊的信息。</li></ul><p><strong>通配符：</strong></p><ol><li>% : 代表任意数量的字符，0个字符也可以。</li></ol><ul><li><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询student_info表中name以“王”开头的记录</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number,name,id_number FROM student_info WHERE name LIKE &#39;王%&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 查询student_info表中name里包含一个“三”字</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number,name,id_number FROM student_info WHERE name LIKE &#39;%三%&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></li></ul><ol start="2"><li>_ :代表任意一个字符</li></ol><ul><li><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询student_info表中name中以“王”开头，且只有2个字符的记录</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number,name,id_number FROM student_info WHERE name LIKE &#39;王_&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 查询student_info表中name是2个字符的记录</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT number,name,id_number FROM student_info WHERE name LIKE &#39;__&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div></li></ul><ol start="3"><li>通配符不能代表NULL</li></ol><p><strong>转移通配符：</strong></p><p>若待匹配字符串中包含普通字符串&#39;%&#39;或&#39;_&#39;的话，需要在其前加一个反斜杠()来和通配符区别。</p><ul><li>&#39;%&#39;代表普通字符串&#39;%&#39;</li><li>&#39;\ _&#39;代表普通字符串&#39; _&#39;</li></ul><h2 id="分组查询" tabindex="-1">分组查询 <a class="header-anchor" href="#分组查询" aria-hidden="true">#</a></h2><ul><li>所谓分组，就是针对某个列，将该列的值相同的记录分到一个组中。</li></ul><h3 id="创建分组" tabindex="-1">创建分组 <a class="header-anchor" href="#创建分组" aria-hidden="true">#</a></h3><ul><li>GROUP BY子句可完成分组</li><li>只需把分组列放在GROUP BY子句中，然后在SELECT子句的查询列表中写入想要统计的信息。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 语法</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT 字段列表 FROM 表名 [WHERE 条件] GROUP BY 分组字段名 [HAVING 分组后过滤条件];</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- WHERE和HAVING的区别</span></span>
<span class="line"><span style="color:#A6ACCD;">⚪执行时机不同：where是分组之前进行过滤，不满足where条件，不参与分组；而having是分组后对结果进行过滤。</span></span>
<span class="line"><span style="color:#A6ACCD;">⚪判断条件不同：where不能对聚合函数进行判断，而having可以。</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 根据性别分组，统计男性员工和女性员工的数量</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT gender,count(*) FROM student_info GROUP BY gender;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><ul><li>查询列表处，不可放置非分组列</li></ul><h3 id="带有where的分组查询" tabindex="-1">带有WHERE的分组查询 <a class="header-anchor" href="#带有where的分组查询" aria-hidden="true">#</a></h3><ul><li>在划分分组之前将某些记录过滤掉，可使用WHERE子句</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 把分数低于60分的记录删去再统计平均分</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT subject,AVG(score) FROM student_score WHERE score &gt;= 60 GROUP BY subject;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="作用于分组的过滤条件" tabindex="-1">作用于分组的过滤条件 <a class="header-anchor" href="#作用于分组的过滤条件" aria-hidden="true">#</a></h3><ul><li>对分组后的数据进行过滤，可以使用 HAVING子句</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 统计平均分大于73分的课程</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT subject,AVG(score) FROM student_score GROUP BY subject HAVING AVG(score) &gt; 73;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 统计“JAVA语言”这门课程的平均分</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT subject,AVG(score) FROM student_score GROUP BY subject HAVING subject = &#39;JAVA语言&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li>HAVING子句中与汇总函数有关的条件是不能出现在WHERE子句中。</li><li>因为汇总函数组成的表达式不适用于判断单条记录是否符合条件。</li></ul><h3 id="分组和排序" tabindex="-1">分组和排序 <a class="header-anchor" href="#分组和排序" aria-hidden="true">#</a></h3><ul><li>分组查询的结果是可以进行排序的</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 按照从大到小的顺序对各个学科的平均分进行排序</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT subject,AVG(score) FROM student_score GROUP BY subject ORDER BY AVG(score) DESC;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="多个分组列" tabindex="-1">多个分组列 <a class="header-anchor" href="#多个分组列" aria-hidden="true">#</a></h3><ul><li>在书写包含嵌套分组的查询语句时，只需要在GROUP BY子句中把各个分组列依次写上，然后用逗号分隔开就好。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 示例</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT department,major,COUNT(*) FROM student_info GROUP BY department,major;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="注意事项-1" tabindex="-1">注意事项 <a class="header-anchor" href="#注意事项-1" aria-hidden="true">#</a></h3><ol><li>如果分组列中有NULL，那么NULL会作为一个独立的分组存在</li><li>GROUP BY子句后可以跟随表达式（但不能是汇总函数）</li></ol><h1 id="dcl-控制数据的权限" tabindex="-1">DCL：控制数据的权限 <a class="header-anchor" href="#dcl-控制数据的权限" aria-hidden="true">#</a></h1><h2 id="dcl介绍" tabindex="-1">DCL介绍 <a class="header-anchor" href="#dcl介绍" aria-hidden="true">#</a></h2><p>DCL英文全称是Data Control Language(数据控制语言)，用来管理数据库用户、控制数据库的访问权限。</p><h2 id="用户管理" tabindex="-1">用户管理 <a class="header-anchor" href="#用户管理" aria-hidden="true">#</a></h2><h3 id="语法" tabindex="-1">语法 <a class="header-anchor" href="#语法" aria-hidden="true">#</a></h3><p>1、查询用户</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">USER mysql;</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM user;</span></span>
<span class="line"><span style="color:#A6ACCD;">或者</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM mysql.user;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>2、创建用户</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE USER &#39;用户名&#39;@&#39;主机名&#39; IDENTIFIED BY &#39;密码&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>3、修改用户密码</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">ALTER USER &#39;用户名&#39;@&#39;主机名&#39; IDENTIFIED WITH mysql_native_password BY &#39;新密码&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">或者</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER USER &#39;用户名&#39;@&#39;主机名&#39; IDENTIFIED BY &#39;新密码&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>4、删除用户</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DROP USER &#39;用户名&#39;@&#39;主机名&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="创建用户" tabindex="-1">创建用户 <a class="header-anchor" href="#创建用户" aria-hidden="true">#</a></h3><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE USER &#39;用户名&#39;@&#39;主机名&#39; IDENTIFIED BY &#39;密码&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>在创建用户时，要指定下面三项内容</p><ul><li>用户名</li><li>主机名——指明客户端可以在那些机器上启动（也可以填入IP地址），若省略则默认为&#39;%&#39;，表示该用户可以在任意主机上启动客户端。</li><li>密码——若省略则表示此用户不需要密码。</li></ul><p>注意：</p><p>刚创建的用户权限很小。</p><h2 id="权限管理" tabindex="-1">权限管理 <a class="header-anchor" href="#权限管理" aria-hidden="true">#</a></h2><h3 id="授予权限" tabindex="-1">授予权限 <a class="header-anchor" href="#授予权限" aria-hidden="true">#</a></h3><h4 id="格式" tabindex="-1">格式 <a class="header-anchor" href="#格式" aria-hidden="true">#</a></h4><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">GRANT 权限名称</span></span>
<span class="line"><span style="color:#A6ACCD;">ON 应用级别</span></span>
<span class="line"><span style="color:#A6ACCD;">TO &#39;用户名&#39;@&#39;主机名&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">[WITH GRANT OPTION];</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h4 id="权限名称" tabindex="-1">权限名称 <a class="header-anchor" href="#权限名称" aria-hidden="true">#</a></h4><p>具体可参考<a href="https://dev.mysql.com/doc/refman/8.0/en/privileges-provided.html#priv_all" target="_blank" rel="noreferrer">MySQL ：： MySQL 8.0 参考手册 ：： 6.2.2 MySQL 提供的权限</a></p><p>以下为MySQL中常用的几种权限</p><table><thead><tr><th style="text-align:center;">权限名称</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">ALL [PRIVILEGES]</td><td style="text-align:center;">代表了除GRANT OPTION、PROXY以外的其他所有权限</td></tr><tr><td style="text-align:center;">SELECT</td><td style="text-align:center;">查询数据</td></tr><tr><td style="text-align:center;">INSERT</td><td style="text-align:center;">插入数据</td></tr><tr><td style="text-align:center;">UPDATE</td><td style="text-align:center;">修改数据</td></tr><tr><td style="text-align:center;">DELETE</td><td style="text-align:center;">删除数据</td></tr><tr><td style="text-align:center;">ALTER</td><td style="text-align:center;">修改表</td></tr><tr><td style="text-align:center;">DROP</td><td style="text-align:center;">删除数据库/表/视图</td></tr><tr><td style="text-align:center;">CREATE</td><td style="text-align:center;">创建数据库/表</td></tr></tbody></table><h4 id="应用级别" tabindex="-1">应用级别 <a class="header-anchor" href="#应用级别" aria-hidden="true">#</a></h4><p>常用级别有以下方式</p><ul><li>*.* ：代表全局级别。全局级别的权限作用于任何数据库下的任何对象</li><li>数据库名.* ：代表数据库级别。数据库级别的权限作用于指定数据库下的任意对象</li><li>数据库名.表名 ：代表表级别。表级别的权限作用于表中的任何列</li></ul><h4 id="with-grant-option" tabindex="-1">WITH GRANT OPTION <a class="header-anchor" href="#with-grant-option" aria-hidden="true">#</a></h4><p>若在使用GREANT语句为某个用户授予权限时添加了WITH GRANT OPTION子句，则表示该用户可以将自己拥有的权限赋予其他人。</p><h3 id="查看权限" tabindex="-1">查看权限 <a class="header-anchor" href="#查看权限" aria-hidden="true">#</a></h3><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW GRANTS FOR &#39;用户名&#39;@&#39;主机名&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="移除权限" tabindex="-1">移除权限 <a class="header-anchor" href="#移除权限" aria-hidden="true">#</a></h3><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">REVOKE 权限名称</span></span>
<span class="line"><span style="color:#A6ACCD;">ON 应用级别</span></span>
<span class="line"><span style="color:#A6ACCD;">FROM &#39;用户名&#39;@&#39;主机名&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><h1 id="函数" tabindex="-1">函数 <a class="header-anchor" href="#函数" aria-hidden="true">#</a></h1><p><strong>函数</strong> 是指一段可以直接被另一端程序调用的程序或代码。</p><p><a href="https://www.runoob.com/mysql/mysql-functions.html" target="_blank" rel="noreferrer">MySQL 函数 | 菜鸟教程 (runoob.com)</a></p><h2 id="汇总函数" tabindex="-1">汇总函数 <a class="header-anchor" href="#汇总函数" aria-hidden="true">#</a></h2><h3 id="max-表达式" tabindex="-1">MAX(表达式) <a class="header-anchor" href="#max-表达式" aria-hidden="true">#</a></h3><ul><li>从匹配的结果中返回表达式对应列的<u>最大值</u>。</li></ul><div class="language-MYSQL line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">MYSQL</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 从student_score表中找出科目为“JAVA语言”的最高成绩</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT MAX(score) FROM student_score WHERE subject = &#39;JAVA语言&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="min-表达式" tabindex="-1">MIN(表达式) <a class="header-anchor" href="#min-表达式" aria-hidden="true">#</a></h3><ul><li>从匹配的结果中返回表达式对应列的<u>最小值</u>。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 从student_score表中找到科目为“JAVA语言”的最低成绩</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT MIN(score) FROM student_score WHERE subject = &#39;JAVA语言&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="sum-表达式" tabindex="-1">SUM(表达式) <a class="header-anchor" href="#sum-表达式" aria-hidden="true">#</a></h3><ul><li>从匹配的结果中计算表达式对应列的<u>总和</u>。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 计算student_score表中科目为“JAVA语言”的成绩的总和</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT SUM(score) FROM student_score WHERE subject = &#39;JAVA语言&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="avg函数" tabindex="-1">AVG函数 <a class="header-anchor" href="#avg函数" aria-hidden="true">#</a></h3><ul><li>从匹配的结果中计算表达式对应列的<u>平均数</u>。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 计算student_score表中科目为“JAVA语言”的成绩的平均分</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT AVG(score) FROM student_score WHERE subject = &#39;JAVA语言&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="count函数" tabindex="-1">COUNT函数 <a class="header-anchor" href="#count函数" aria-hidden="true">#</a></h3><ul><li>从匹配的结果中统计表达式对应列中<u>非NULL值的数量</u>。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 使用COUNT函数统计student表列id中非NULL值的数量</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT COUNT(id) FROM student;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 查询匹配的结果中总共有多少条记录</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT COUNT(*) FROM student;</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 当然，除了 * ，也可以使用任何值不为NULL的表达式</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="汇总函数中的distinct" tabindex="-1">汇总函数中的DISTINCT <a class="header-anchor" href="#汇总函数中的distinct" aria-hidden="true">#</a></h3><ul><li>可使用DISTINCT函数来过滤重复数据</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查看student_info表中存储了多少个专业的学生信息</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT COUNT(DISTINCT major) FROM student_info;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="使用多个汇总函数" tabindex="-1">使用多个汇总函数 <a class="header-anchor" href="#使用多个汇总函数" aria-hidden="true">#</a></h3><ul><li>多个汇总函数可放在同一个查询列表中。</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT COUNT(*) AS 成绩记录总数, MAX(score) AS 最高成绩, MIN(score) AS 最低成绩, AVG(score) AS 平均成绩 FROM student_score;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="字符串函数" tabindex="-1">字符串函数 <a class="header-anchor" href="#字符串函数" aria-hidden="true">#</a></h2><ul><li>常用函数</li></ul><table><thead><tr><th style="text-align:center;">名称</th><th style="text-align:center;">示例</th><th style="text-align:center;">示例结果</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">CONCAT</td><td style="text-align:center;">CONCAT(&#39;abc&#39;,&#39;123&#39;,&#39;xyz&#39;)</td><td style="text-align:center;">abc123xyz</td><td style="text-align:center;">拼接字符串</td></tr><tr><td style="text-align:center;">LOWER</td><td style="text-align:center;">LOWER(&#39;ABC&#39;)</td><td style="text-align:center;">abc</td><td style="text-align:center;">转小写</td></tr><tr><td style="text-align:center;">UPPER</td><td style="text-align:center;">UPPER(&#39;abc&#39;)</td><td style="text-align:center;">ABC</td><td style="text-align:center;">转大写</td></tr><tr><td style="text-align:center;">LEFT</td><td style="text-align:center;">LEFT(&#39;abc123&#39;,3)</td><td style="text-align:center;">abc</td><td style="text-align:center;">从左取指定长度</td></tr><tr><td style="text-align:center;">RIGHT</td><td style="text-align:center;">RIGHT(&#39;abc123&#39;,3)</td><td style="text-align:center;">123</td><td style="text-align:center;">从右取指定长度</td></tr><tr><td style="text-align:center;">TRIM</td><td style="text-align:center;">TRIM(&#39; abc &#39;)</td><td style="text-align:center;">abc</td><td style="text-align:center;">去除两边空格</td></tr><tr><td style="text-align:center;">SUBSTRING</td><td style="text-align:center;">SUBSTRING(&#39;abc123&#39;,2,3)</td><td style="text-align:center;">bc1</td><td style="text-align:center;">指定位置截取指定长度</td></tr><tr><td style="text-align:center;">LPAD</td><td style="text-align:center;">LPAD(str,n,pad)</td><td style="text-align:center;"></td><td style="text-align:center;">左填充,用pad对str填充至n个长度</td></tr></tbody></table><ul><li>使用方法</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 函数(参数);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="数值函数" tabindex="-1">数值函数 <a class="header-anchor" href="#数值函数" aria-hidden="true">#</a></h2><ul><li>常用函数</li></ul><table><thead><tr><th style="text-align:center;">名称</th><th style="text-align:center;">示例</th><th style="text-align:center;">示例结果</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">ABS</td><td style="text-align:center;">ABS(-1)</td><td style="text-align:center;">1</td><td style="text-align:center;">取绝对值</td></tr><tr><td style="text-align:center;">MOD</td><td style="text-align:center;">MOD(5,2)</td><td style="text-align:center;">1</td><td style="text-align:center;">返回除法的余数</td></tr><tr><td style="text-align:center;">RAND</td><td style="text-align:center;">RAND()</td><td style="text-align:center;"></td><td style="text-align:center;">返回一个随机数</td></tr><tr><td style="text-align:center;">CEIL</td><td style="text-align:center;">CEIL(2.3)</td><td style="text-align:center;">3</td><td style="text-align:center;">返回不小于给定值的最小整数</td></tr><tr><td style="text-align:center;">FLOOR</td><td style="text-align:center;">FLOOR(2.3)</td><td style="text-align:center;">2</td><td style="text-align:center;">返回不大于给定值的最大整数</td></tr><tr><td style="text-align:center;">SQRT</td><td style="text-align:center;">SQRT(9)</td><td style="text-align:center;">3</td><td style="text-align:center;">返回平方根</td></tr><tr><td style="text-align:center;">POW</td><td style="text-align:center;">POW(2,2)</td><td style="text-align:center;">4</td><td style="text-align:center;">返回定次幂</td></tr><tr><td style="text-align:center;">Pi</td><td style="text-align:center;">Pi()</td><td style="text-align:center;"></td><td style="text-align:center;">返回圆周率</td></tr><tr><td style="text-align:center;">ROUND(x,y)</td><td style="text-align:center;">ROUND(2.344,2)</td><td style="text-align:center;">2.23</td><td style="text-align:center;">求参数四舍五入后的值,保留y位小数</td></tr></tbody></table><h2 id="日期函数" tabindex="-1">日期函数 <a class="header-anchor" href="#日期函数" aria-hidden="true">#</a></h2><ul><li>常用函数</li></ul><table><thead><tr><th style="text-align:center;">名称</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">NOW()</td><td style="text-align:center;">返回当前时间和日期</td></tr><tr><td style="text-align:center;">CURDATE()</td><td style="text-align:center;">返回当前日期</td></tr><tr><td style="text-align:center;">CURTIME()</td><td style="text-align:center;">返回当前时间</td></tr><tr><td style="text-align:center;">DATE(date)</td><td style="text-align:center;">提取date的日期</td></tr><tr><td style="text-align:center;">DATE_ADD(date,INTERVAL expr type)</td><td style="text-align:center;">返回date加上expr type后的时间值</td></tr><tr><td style="text-align:center;">DATE_SUB(date,INTERVAL expr type)</td><td style="text-align:center;">返回date减去expr type后的时间值</td></tr><tr><td style="text-align:center;">DATEDIFF(date1,date2)</td><td style="text-align:center;">返回date1减去date2后的天数</td></tr><tr><td style="text-align:center;">DATE_FORMAT(date,&#39;格式&#39;)</td><td style="text-align:center;">用给定格式显示日期和时间</td></tr><tr><td style="text-align:center;">YEAR(date)</td><td style="text-align:center;">提取年份</td></tr><tr><td style="text-align:center;">MONTH(date)</td><td style="text-align:center;">提取月份</td></tr><tr><td style="text-align:center;">DAY(date)</td><td style="text-align:center;">提取日</td></tr><tr><td style="text-align:center;">HOUR(date)</td><td style="text-align:center;">提取小时</td></tr><tr><td style="text-align:center;">MINUTE(date)</td><td style="text-align:center;">提取分钟</td></tr><tr><td style="text-align:center;">SECOND(date)</td><td style="text-align:center;">提取秒</td></tr></tbody></table><ul><li>其中DATE_ADD和DATE_SUB所用的type为时间单位</li><li>DATE_FORMAT(NOW(),&#39;%m-%d-%Y&#39;);</li></ul><h2 id="流程函数" tabindex="-1">流程函数 <a class="header-anchor" href="#流程函数" aria-hidden="true">#</a></h2><h3 id="case-一" tabindex="-1">CASE 一 <a class="header-anchor" href="#case-一" aria-hidden="true">#</a></h3><ul><li>语法</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CASE WHEN 表达式1 THEN 结果1 [WHEN 表达式2 THEN 结果2 ...] [ELSE 默认结果] END</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>示例</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT number,subject,score CASE WHEN score&lt;60 THEN &#39;不及格&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">								 WHEN score&lt;90 THEN &#39;及格&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">								 ELSE &#39;优秀&#39; END AS level</span></span>
<span class="line"><span style="color:#A6ACCD;">							FORM student_score;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li>含义 <ul><li>当表达式1为true，返回结果1</li><li>当表达式2为true，返回结果2</li><li>...</li><li>若都不为true，返回ELSE的默认结果</li></ul></li></ul><h3 id="case-二" tabindex="-1">CASE 二 <a class="header-anchor" href="#case-二" aria-hidden="true">#</a></h3><ul><li>语法</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CASE 待比较表达式 WHEN 表达式1 THEN 结果1 [WHEN 表达式2 THEN 结果2 ...] [ELSE 默认结果] END</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>示例</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>含义 <ul><li>当待比较表达式的值和表达式1的值相同，返回结果1</li><li>当待比较表达式的值和表达式2的值相同，返回结果2</li><li>...</li><li>若都不为true，返回ELSE的默认结果</li></ul></li></ul><h3 id="if" tabindex="-1">IF <a class="header-anchor" href="#if" aria-hidden="true">#</a></h3><ul><li>语法</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">IF(表达式1,表达式2,表达式3)</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>示例</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">IF(1&gt;2,3,4)</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 结果为4</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>含义 <ul><li>当表达式1为TRUE时，返回值为表达式2，否则为表达式3</li></ul></li></ul><h3 id="ifnull" tabindex="-1">IFNULL <a class="header-anchor" href="#ifnull" aria-hidden="true">#</a></h3><ul><li>语法</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">IFNULL(表达式1,表达式2)</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>示例</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT IFNULL(NULL,5);</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 结果为5</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT IFNULL(3,5);</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 结果为3</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li>含义 <ul><li>当表达式1为NULL，返回值为表达式2，否则为表达式1</li></ul></li></ul><h3 id="nullif" tabindex="-1">NULLIF <a class="header-anchor" href="#nullif" aria-hidden="true">#</a></h3><ul><li>语法</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">NULLIF(表达式1,表达式2)</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>示例</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT NULLIF(2,3);</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 结果为2</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT NULLIF(2,2);</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 结果为NULL</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><ul><li>含义 <ul><li>当表达式1和表达式2的值相同时，返回值为NULL，否则返回表达式1的值</li></ul></li></ul><h1 id="约束" tabindex="-1">约束 <a class="header-anchor" href="#约束" aria-hidden="true">#</a></h1><h2 id="概述" tabindex="-1">概述 <a class="header-anchor" href="#概述" aria-hidden="true">#</a></h2><p><strong>概念</strong></p><p>​ 约束是作用于表中字段上的规则，用于限制存储在表中的数据。</p><p><strong>目的</strong></p><p>​ 保证数据库中数据的正确、有效性和完整性。</p><p><strong>分类</strong></p><table><thead><tr><th style="text-align:center;">约束</th><th style="text-align:center;">描述</th><th style="text-align:center;">关键字</th></tr></thead><tbody><tr><td style="text-align:center;">非空约束</td><td style="text-align:center;">限制该字段的数据不能为null</td><td style="text-align:center;">NOT NULL</td></tr><tr><td style="text-align:center;">唯一约束</td><td style="text-align:center;">保证该字段的所有数据都是唯一、不重复的</td><td style="text-align:center;">UNIQUE</td></tr><tr><td style="text-align:center;">主键约束</td><td style="text-align:center;">主键是一行数据的唯一标识，要求非空且唯一</td><td style="text-align:center;">PRIMARY KEY</td></tr><tr><td style="text-align:center;">默认约束</td><td style="text-align:center;">保存数据时，如果未指定该字段的值，则采用默认值</td><td style="text-align:center;">DEFAULT</td></tr><tr><td style="text-align:center;">检查约束</td><td style="text-align:center;">保证字段值满足某个条件</td><td style="text-align:center;">CHECK</td></tr><tr><td style="text-align:center;">外键约束</td><td style="text-align:center;">用来让两张表的数据之间建立连接，保证数据的一致性和完整性</td><td style="text-align:center;">FOREIGN KEY</td></tr></tbody></table><p><strong>注意</strong>：约束是作用于表中字段上的，可以在创建表/修改表的时候添加约束。</p><h2 id="约束的使用" tabindex="-1">约束的使用 <a class="header-anchor" href="#约束的使用" aria-hidden="true">#</a></h2><h3 id="案例-根据需求完成表结构的创建" tabindex="-1">案例：根据需求完成表结构的创建 <a class="header-anchor" href="#案例-根据需求完成表结构的创建" aria-hidden="true">#</a></h3><table><thead><tr><th style="text-align:center;">字段名</th><th style="text-align:center;">字段含义</th><th style="text-align:center;">字段类型</th><th style="text-align:center;">约束条件</th><th style="text-align:center;">关键字</th></tr></thead><tbody><tr><td style="text-align:center;">id</td><td style="text-align:center;">ID唯一标识</td><td style="text-align:center;">int</td><td style="text-align:center;">主键，自动增长</td><td style="text-align:center;">PRIMARY KEY，AUTO_INCREMENT</td></tr><tr><td style="text-align:center;">name</td><td style="text-align:center;">姓名</td><td style="text-align:center;">varchar(10)</td><td style="text-align:center;">不为空，且唯一</td><td style="text-align:center;">NOT NTLL，UNIQUE</td></tr><tr><td style="text-align:center;">age</td><td style="text-align:center;">年龄</td><td style="text-align:center;">int</td><td style="text-align:center;">大于0，且小于等于120</td><td style="text-align:center;">CHECK</td></tr><tr><td style="text-align:center;">status</td><td style="text-align:center;">状态</td><td style="text-align:center;">char(1)</td><td style="text-align:center;">默认为1</td><td style="text-align:center;">DEFAULT</td></tr><tr><td style="text-align:center;">gender</td><td style="text-align:center;">性别</td><td style="text-align:center;">char(1)</td><td style="text-align:center;">无</td><td style="text-align:center;"></td></tr></tbody></table><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">create table user(</span></span>
<span class="line"><span style="color:#A6ACCD;">	id int primary key auto_increment comment &#39;主键&#39;,</span></span>
<span class="line"><span style="color:#A6ACCD;">    name varchar(10) not null unique comment &#39;姓名&#39;,</span></span>
<span class="line"><span style="color:#A6ACCD;">    age int check (age&gt;0 &amp;&amp; age&lt;=120) comment &#39;年龄&#39;,</span></span>
<span class="line"><span style="color:#A6ACCD;">    status char(1) default &#39;1&#39; comment &#39;状态&#39;,</span></span>
<span class="line"><span style="color:#A6ACCD;">    gender char(1) comment &#39;性别&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">) comment &#39;用户表&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h2 id="外键约束" tabindex="-1">外键约束 <a class="header-anchor" href="#外键约束" aria-hidden="true">#</a></h2><h3 id="语法-1" tabindex="-1">语法 <a class="header-anchor" href="#语法-1" aria-hidden="true">#</a></h3><ul><li>添加外键</li></ul><p><strong>一</strong>、创建表时，添加外键约束</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE TABLE 表名(</span></span>
<span class="line"><span style="color:#A6ACCD;">	字段名 数据类型,</span></span>
<span class="line"><span style="color:#A6ACCD;">    ...</span></span>
<span class="line"><span style="color:#A6ACCD;">    [CONSTRAINT] [外键名称] FOREIGN KEY (外键字段名) REFERENCES 主表(主表列名)</span></span>
<span class="line"><span style="color:#A6ACCD;">);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p><strong>二</strong>、表已创建，更新表时添加外键约束</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段名) REFERENCES 主表(主表列名);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>删除外键</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 DROP FOREIGN KEY 外键名称;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="删除和更新行为" tabindex="-1">删除和更新行为 <a class="header-anchor" href="#删除和更新行为" aria-hidden="true">#</a></h3><h4 id="行为" tabindex="-1">行为 <a class="header-anchor" href="#行为" aria-hidden="true">#</a></h4><table><thead><tr><th style="text-align:center;">行为</th><th style="text-align:center;">说明</th></tr></thead><tbody><tr><td style="text-align:center;">NO ACTION</td><td style="text-align:center;">当在父表中删除/更新对应记录时，首先检查记录是否有对应外键，若有则不允许删除/更新。（与RESTRICT一致）</td></tr><tr><td style="text-align:center;">RESTRICT</td><td style="text-align:center;">当在父表中删除/更新对应记录时，首先检查记录是否有对应外键，若有则不允许删除/更新。（与NO ACTION一致）</td></tr><tr><td style="text-align:center;">CASCADE</td><td style="text-align:center;">当在父表中删除/更新对应记录时，首先检查记录是否有对应外键，若有，则也删除/更新外键在子表中的记录。</td></tr><tr><td style="text-align:center;">SET NULL</td><td style="text-align:center;">当在父表中删除/更新对应记录时，首先检查记录是否有对应外键，若有则设置子表中该外键为null（要求该外键允许null）</td></tr><tr><td style="text-align:center;">SET DEFAULT</td><td style="text-align:center;">父表有变更时，子表将外键设置成一个默认的值（lnnodb不支持）</td></tr></tbody></table><h4 id="语法-2" tabindex="-1">语法 <a class="header-anchor" href="#语法-2" aria-hidden="true">#</a></h4><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">例如（CASCADE操作）：</span></span>
<span class="line"><span style="color:#A6ACCD;">ALTER TABLE 表名 ADD CONSTRAINT 外键名称 FOREIGN KEY (外键字段) REFERENCES 主表名(主表字段名) ON UPDATE CASCADE ON DELETE CASCADE;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h1 id="多表查询" tabindex="-1">多表查询 <a class="header-anchor" href="#多表查询" aria-hidden="true">#</a></h1><h2 id="概述-1" tabindex="-1">概述 <a class="header-anchor" href="#概述-1" aria-hidden="true">#</a></h2><ul><li>多表查询指从多张表中查询数据</li><li>笛卡尔积：指在数学中，两个集合 集合A和集合B的所有组合情况。（在多表查询时，需要显出无效的笛卡尔积）</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">如：</span></span>
<span class="line"><span style="color:#A6ACCD;">select * from emp,dept;</span></span>
<span class="line"><span style="color:#A6ACCD;">上为多表查询</span></span>
<span class="line"><span style="color:#A6ACCD;">将所有结果都展现，结果不符合预期</span></span>
<span class="line"><span style="color:#A6ACCD;">可写成：</span></span>
<span class="line"><span style="color:#A6ACCD;">select * from emp,dept where emp.dept_id = dept.id;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>多表查询分类 <ul><li>连接查询 <ul><li>内连接：相当于查询集合A、集合B交集部分数据</li><li>外连接： <ul><li>左外连接：查询左表所有数据，以及两张表交集部分数据</li><li>右外连接：查询右表所有数据，以及两张表交集部分数据</li></ul></li><li>自连接：当前表与自身的连接查询，自连接必须使用表别名</li></ul></li><li>子查询</li></ul></li></ul><h2 id="内、外、自连接" tabindex="-1">内、外、自连接 <a class="header-anchor" href="#内、外、自连接" aria-hidden="true">#</a></h2><h3 id="概念" tabindex="-1">概念 <a class="header-anchor" href="#概念" aria-hidden="true">#</a></h3><ul><li>对于<u>内连接</u>的两个表，若驱动表中的记录在被驱动表中找不到匹配的记录，则该记录不会加入到最后的结果集中。</li><li>对于<u>外连接</u>的两个表，即使驱动表中的记录在被驱动表中没有被匹配的记录，也仍然需要加入到结果集中。</li><li>对于<u>自连接</u>的两个表，由于驱动表和被驱动表是一个表，故称为自连接。</li></ul><h3 id="on子句" tabindex="-1">ON子句 <a class="header-anchor" href="#on子句" aria-hidden="true">#</a></h3><p>在WHERE子句之外，又引入了ON子句</p><ul><li><p>WHERE子句中的过滤条件</p><p>不论是内连接还是外连接，凡是不符合WHERE子句中过滤条件的记录都不会被加入最后的结果集。</p></li><li><p>ON子句中的过滤条件</p><p>对于外连接的驱动表的记录来说，若无法在被驱动表中找到匹配ON子句中过滤条件的记录，那么该驱动表记录仍然会被加入到结果集中，对应的被驱动表记录的各个字段使用NULL值填充。</p></li></ul><p>**注意：**ON子句的特殊性只有在外连接查询中才会体现。</p><h3 id="内连接" tabindex="-1">内连接 <a class="header-anchor" href="#内连接" aria-hidden="true">#</a></h3><p>内连接查询的就是两张表交集的部分</p><p><strong>语法：</strong></p><ul><li>隐式内连接</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 字段列表 FROM 表1,表2 [WHERE 条件...];</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 查询每一个员工的姓名，及关联的部门的名称</span></span>
<span class="line"><span style="color:#A6ACCD;">select emp.name,dept.name from emp,dept where emp.dept_id = dept.id;</span></span>
<span class="line"><span style="color:#A6ACCD;">||   下为起别名后的用法</span></span>
<span class="line"><span style="color:#A6ACCD;">select e.name,d.name from emp e,dept d where e.dept_id = d.id;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li>显示内连接</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 字段列表 FROM 表1 [INNER|CROSS] JOIN 表2 [ON 条件...] [WHERE 条件...];</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 推荐使用INNER</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 查询每一个员工的姓名，及关联的部门的名称</span></span>
<span class="line"><span style="color:#A6ACCD;">select emp.name,dept.name from emp inner join dept on emp.dept_id = dept.in;</span></span>
<span class="line"><span style="color:#A6ACCD;">select e.name,d.name from emp e inner join dept d on e.dept_id = d.id;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><h3 id="外连接" tabindex="-1">外连接 <a class="header-anchor" href="#外连接" aria-hidden="true">#</a></h3><p><strong>语法:</strong></p><ul><li>左（外）连接</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 字段列表 FROM 表1 LEFT [OUTER] JOIN 表2 ON 过滤条件  [WHERE 过滤条件];</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 其中表1为外表或驱动表，表2为内表或被驱动表</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>其中LEFT [OUTER] JOIN<u>左侧</u>的表称为外表或驱动表，<u>右侧</u>的表称为内表或被驱动表</p><p>相当于查询表1（左表）的所有数据，包含表1和表2交集部分的数据</p><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询emp表的所有数据，和对应的部门信息（左外连接）</span></span>
<span class="line"><span style="color:#A6ACCD;">select e.*,d.name from emp e left outer join dept d on e.dept_id = d.id;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li>右（外）连接</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 字段列表 FROM 表1 RIGHT [OUTER] JOIN 表2 ON 过滤条件  [WHERE 过滤条件];</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 其中表2为外表或驱动表，表1为内表或被驱动表</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>其中LEFT [OUTER] JOIN<u>右侧</u>的表称为外表或驱动表，<u>左侧</u>的表称为内表或被驱动表</p><p>相当于查询表2（右表）的所有数据，包含表1和表2交集部分的数据</p><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询dept表的所有数据，和对应的部门信息（右外连接）</span></span>
<span class="line"><span style="color:#A6ACCD;">select d.*,e.* from emp e right outer join dept d on e.dept_id = d.id;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="自连接" tabindex="-1">自连接 <a class="header-anchor" href="#自连接" aria-hidden="true">#</a></h3><p><strong>语法：</strong></p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 字段列表 FROM 表A 别名A [INNER] JOIN 表A 别名B ON 条件...;</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 因为FROM子句中不能出现相同的表名，故需要对表定义别名</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 例如</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM t1 AS table1 ,t1 AS table2;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>自连接查询，可以是内连接查询，也可以是外连接查询。</p><h2 id="联合查询" tabindex="-1">联合查询 <a class="header-anchor" href="#联合查询" aria-hidden="true">#</a></h2><ul><li>对于联合查询，就是把多次查询的结果合并起来，形成一个新的查询结果集。</li></ul><h3 id="union" tabindex="-1">UNION <a class="header-anchor" href="#union" aria-hidden="true">#</a></h3><ul><li>UNION可以将两个查询结果连在一起</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 字段列表 FROM 表A ... UNION SELECT 字段列表 FROM 表B ...;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>例如：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 将下两个查询语句联合起来</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT m1 FROM t1 WHERE m1&lt;2;</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT m1 FROM t1 WHERE m1&gt;2;</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT m1 FROM t1 WHERE m1&lt;2 UNION SELECT m1 FROM t1 WHERE m1&lt;2;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="union-all" tabindex="-1">UNION ALL <a class="header-anchor" href="#union-all" aria-hidden="true">#</a></h3><ul><li>UNION会默认过滤结果集中重复的记录</li><li>UNION ALL 会在连接多个查询的同时保留重复记录</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT 字段列表 FROM 表A ... UNION ALL SELECT 字段列表 FROM 表B ...;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="子查询" tabindex="-1">子查询 <a class="header-anchor" href="#子查询" aria-hidden="true">#</a></h2><ul><li>概念</li></ul><p>子查询：在SQL语句中嵌套SELECT语句，称为嵌套查询，又称为内层查询。</p><p>外层查询：使用子查询的结果作为搜索条件的查询称为外层查询。</p><ul><li><p>根据子查询结果不同，分为：</p><ul><li>标量子查询（子查询结果为单个值）</li><li>列子查询（子查询结果为一列）</li><li>列子查询（子查询结果为一行）</li><li>表子查询（子查询结果为多行多列）</li></ul></li><li><p>根据子查询位置不同，分为：WHERE之后、FROM之后、SELECT之后。</p></li></ul><p>在执行查询语句时，将按照从内到外的顺序依次执行。</p><h3 id="标量子查询" tabindex="-1">标量子查询 <a class="header-anchor" href="#标量子查询" aria-hidden="true">#</a></h3><ul><li>概念</li></ul><p>子查询返回的结果只有单个值（数字、字符串、日期等），这种子查询称为标量子查询。</p><p>例如：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询“销售部”的所有员工信息</span></span>
<span class="line"><span style="color:#A6ACCD;">方法一：</span></span>
<span class="line"><span style="color:#A6ACCD;">a、先查询“销售部”部门ID</span></span>
<span class="line"><span style="color:#A6ACCD;">select id from dept where name=&#39;销售部&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">b、根据销售部部门ID，查询员工信息</span></span>
<span class="line"><span style="color:#A6ACCD;">select * from emp where dept_id=4;</span></span>
<span class="line"><span style="color:#A6ACCD;">方法二：</span></span>
<span class="line"><span style="color:#A6ACCD;">select * from emp where dept_id=(select id from dept where name=&#39;销售部&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>注：因为标量子查询单纯的代表一个值，故可作为表达式的操作数来进行运算。</p><h3 id="列子查询" tabindex="-1">列子查询 <a class="header-anchor" href="#列子查询" aria-hidden="true">#</a></h3><ul><li>概念</li></ul><p>子查询返回是一列（可能是多行），这种子查询称为列子查询。</p><p>常用的操作符：IN、NOT IN、ANY、SOME、ALL</p><table><thead><tr><th style="text-align:center;">操作符</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">IN</td><td style="text-align:center;">在指定的集合范围之内，多选一</td></tr><tr><td style="text-align:center;">NOT IN</td><td style="text-align:center;">不在指定的集合范围内</td></tr><tr><td style="text-align:center;">ANY</td><td style="text-align:center;">子查询返回列表中，有任意一个满足即可</td></tr><tr><td style="text-align:center;">SOME</td><td style="text-align:center;">与ANY等同，使用SOME的地方都可以使用ANY</td></tr><tr><td style="text-align:center;">ALL</td><td style="text-align:center;">子查询返回列表的所有制都必须满足</td></tr></tbody></table><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询“销售部”和“市场部”的所有员工信息</span></span>
<span class="line"><span style="color:#A6ACCD;">方法一:</span></span>
<span class="line"><span style="color:#A6ACCD;">a、先查询“销售部”和“市场部”的部门ID</span></span>
<span class="line"><span style="color:#A6ACCD;">select id from dept where name = &#39;销售部&#39; or name = &#39;市场部&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">b、再根据部门ID，查询员工信息</span></span>
<span class="line"><span style="color:#A6ACCD;">select * from emp where dept_id in (2,4);</span></span>
<span class="line"><span style="color:#A6ACCD;">方法二：</span></span>
<span class="line"><span style="color:#A6ACCD;">select * from emp where dept_id in (select id from dept where name = &#39;销售部&#39; or name = &#39;市场部&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h3 id="行子查询" tabindex="-1">行子查询 <a class="header-anchor" href="#行子查询" aria-hidden="true">#</a></h3><ul><li>概念</li></ul><p>子查询返回的结果是一行（可以是多列），这种子查询称为行子查询。</p><p>常用的操作符：=、&lt;&gt;、IN、NOT IN</p><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询与“张无忌”的薪资及直属领导相同的员工信息</span></span>
<span class="line"><span style="color:#A6ACCD;">方法一：</span></span>
<span class="line"><span style="color:#A6ACCD;">a、先查询“张无忌”的薪资和直属领导</span></span>
<span class="line"><span style="color:#A6ACCD;">select salary,managerid from emp where name = &#39;张无忌&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">b、再查询与“张无忌”的薪资及直属领导相同的员工信息</span></span>
<span class="line"><span style="color:#A6ACCD;">select * from emp where salary = 12500 and managerid = 1;</span></span>
<span class="line"><span style="color:#A6ACCD;">select * from emp where (salary,managerid) = (12500,1);</span></span>
<span class="line"><span style="color:#A6ACCD;">方法二：</span></span>
<span class="line"><span style="color:#A6ACCD;">select * from emp where (salary,managerid) = (select salary,managerid from emp where name = &#39;张无忌&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><h3 id="表子查询" tabindex="-1">表子查询 <a class="header-anchor" href="#表子查询" aria-hidden="true">#</a></h3><ul><li>概念</li></ul><p>子查询返回的结果是多行多列，这种子查询称为表子查询。</p><p>常用的操作符：IN</p><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查询与“鹿仗客”，“宋远桥”的职位和薪资相同的员工信息</span></span>
<span class="line"><span style="color:#A6ACCD;">a、查询“鹿仗客”，“宋远桥”的职位和薪资</span></span>
<span class="line"><span style="color:#A6ACCD;">select jod,salary from emp where name = &#39;鹿仗客&#39; or name = &#39;宋远桥&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;">b、查询与“鹿仗客”，“宋远桥”的职位和薪资相同的员工信息</span></span>
<span class="line"><span style="color:#A6ACCD;">select * from emp where (job,salary) in (select jod,salary from emp where name = &#39;鹿仗客&#39; or name = &#39;宋远桥&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h3 id="exists子查询" tabindex="-1">EXISTS子查询 <a class="header-anchor" href="#exists子查询" aria-hidden="true">#</a></h3><ul><li>判断子查询的结果集是不是为空集</li></ul><table><thead><tr><th style="text-align:center;">运算符</th><th style="text-align:center;">示例</th><th style="text-align:center;">描述</th></tr></thead><tbody><tr><td style="text-align:center;">EXISTS</td><td style="text-align:center;">EXISTS (SELECT ...)</td><td style="text-align:center;">当子查询结果集<u>不是空集</u>时表达式为真</td></tr><tr><td style="text-align:center;">NOT EXISTS</td><td style="text-align:center;">NOT EXISTS (SELECT ...)</td><td style="text-align:center;">当子查询结果集<u>是空集</u>时表达式为真</td></tr></tbody></table><h3 id="相关子查询" tabindex="-1">相关子查询 <a class="header-anchor" href="#相关子查询" aria-hidden="true">#</a></h3><p>不相关子查询：</p><blockquote><p>子查询可以独立运行并产生结果，之后再拿结果作为外层查询的条件去执行外层查询，这种子查询称为不相关子查询。</p></blockquote><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">select * from student_score where number = (select number from student_info where name = &#39;狗哥&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>相关子查询：</p><blockquote><p>需要在子查询的语句中引入外层查询的列，此时子查询就不能当作一个独立的语句去执行，这种子查询称为相关子查询。</p></blockquote><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">select number,name,id_number,major from student_info where exists (select * from student_score where student_score.number = student_info.number);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h1 id="视图" tabindex="-1">视图 <a class="header-anchor" href="#视图" aria-hidden="true">#</a></h1><p><strong>视图</strong>（View）是一种虚拟存在的表，同真实表一样，视图也由列和行构成，但视图并不实际存在于数据库中。</p><p>可以把视图看作是一个查询语句的别名，即将一个查询语句用一个名字代替。</p><h2 id="创建视图" tabindex="-1">创建视图 <a class="header-anchor" href="#创建视图" aria-hidden="true">#</a></h2><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE VIEW 视图名 AS 查询语句</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">create view male_student_view as select s1.number,s1.name,s1.major,s2.subject,s2.score from student_info as s1 inner join student_score as s2 where s1.number = s2.number and s1.sex = &#39;男&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><table><thead><tr><th style="text-align:center;">number</th><th style="text-align:center;">name</th><th style="text-align:center;">major</th><th style="text-align:center;">subject</th><th style="text-align:center;">score</th></tr></thead><tbody><tr><td style="text-align:center;">20210101</td><td style="text-align:center;">狗哥</td><td style="text-align:center;">计算机科学与工程</td><td style="text-align:center;">MySQL是怎样运行的</td><td style="text-align:center;">88</td></tr><tr><td style="text-align:center;">20210101</td><td style="text-align:center;">狗哥</td><td style="text-align:center;">计算机科学与工程</td><td style="text-align:center;">计算机是怎样运行的</td><td style="text-align:center;">78</td></tr><tr><td style="text-align:center;">20210102</td><td style="text-align:center;">猫爷</td><td style="text-align:center;">计算机科学与工程</td><td style="text-align:center;">MySQL是怎样运行的</td><td style="text-align:center;">98</td></tr><tr><td style="text-align:center;">20210102</td><td style="text-align:center;">猫爷</td><td style="text-align:center;">计算机科学与工程</td><td style="text-align:center;">计算机是怎样运行的</td><td style="text-align:center;">100</td></tr><tr><td style="text-align:center;">20210104</td><td style="text-align:center;">亚索</td><td style="text-align:center;">软件工程</td><td style="text-align:center;">MySQL是怎样运行的</td><td style="text-align:center;">46</td></tr><tr><td style="text-align:center;">20210104</td><td style="text-align:center;">亚索</td><td style="text-align:center;">软件工程</td><td style="text-align:center;">计算机是怎样运行的</td><td style="text-align:center;">55</td></tr></tbody></table><h2 id="使用视图" tabindex="-1">使用视图 <a class="header-anchor" href="#使用视图" aria-hidden="true">#</a></h2><p>视图也可以称为虚拟表，原因是我们可以像表那样对视图进行一些增删改查操作。</p><ul><li>查询视图 <ul><li>平时怎样从表中查询信息，就可以怎么从视图中查询信息</li><li>对视图进行查询时，MySQL服务器会把对视图的查询语句转换为底层表的查询语句，再执行。</li></ul></li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">select * from male_student_view;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>再次强调，视图相当于某个查询语句的别名。</p><h3 id="利用视图来创建新视图" tabindex="-1">利用视图来创建新视图 <a class="header-anchor" href="#利用视图来创建新视图" aria-hidden="true">#</a></h3><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">create view  male_student_view2 as select number,name,score from  male_student_view;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><table><thead><tr><th style="text-align:center;">number</th><th style="text-align:center;">name</th><th style="text-align:center;">score</th></tr></thead><tbody><tr><td style="text-align:center;">...</td><td style="text-align:center;">...</td><td style="text-align:center;">...</td></tr></tbody></table><h3 id="创建视图时自定义列名" tabindex="-1">创建视图时自定义列名 <a class="header-anchor" href="#创建视图时自定义列名" aria-hidden="true">#</a></h3><blockquote><p>在创建视图的时候为其虚拟列自定义列名，这些自定义列名需要用小括号括起来，写在视图名后面，并使用逗号将各个虚拟列的自定义列名分开。</p></blockquote><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 创建一个自定义列名的视图</span></span>
<span class="line"><span style="color:#A6ACCD;">create view student_info_view(no,n,m) as select number,name,major from student_info;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><blockquote><p>有了自定义列名后，之后对视图的查询都要基于自定义的列名</p></blockquote><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">select no,n,m from student_info_view;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="操作视图" tabindex="-1">操作视图 <a class="header-anchor" href="#操作视图" aria-hidden="true">#</a></h2><h3 id="查看视图" tabindex="-1">查看视图 <a class="header-anchor" href="#查看视图" aria-hidden="true">#</a></h3><ul><li>查看现有视图</li></ul><blockquote><p>视图会在默认数据库下</p><p>-- 名字不能冲突</p></blockquote><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW TABLES;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>查看视图的定义</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW CREATE VIEW 视图名\G</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="更新视图" tabindex="-1">更新视图 <a class="header-anchor" href="#更新视图" aria-hidden="true">#</a></h3><blockquote><p>对视图进行增删改操作，其实就是对该视图对应的底层表中的数据进行增、删、改操作。</p></blockquote><h3 id="删除视图" tabindex="-1">删除视图 <a class="header-anchor" href="#删除视图" aria-hidden="true">#</a></h3><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DROP VIEW 视图名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h1 id="存储程序" tabindex="-1">存储程序 <a class="header-anchor" href="#存储程序" aria-hidden="true">#</a></h1><ul><li><strong>存储程序可以封装一些语句</strong>，然后用户可以用简单的语句来调用这个存储函数。</li><li>根据调用方式的不同，可以把存储程序分为： <ul><li>存储例程 <ul><li>存储函数</li><li>存储过程</li></ul></li><li>触发器</li><li>事件</li></ul></li></ul><h2 id="用户自定义变量" tabindex="-1">用户自定义变量 <a class="header-anchor" href="#用户自定义变量" aria-hidden="true">#</a></h2><ul><li>通过SET语句来自定义变量</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SET @a = 1;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">注意：必须在自定义变量前添加 @ 符号</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>查看变量的值</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT @a;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>同一个变量可以存储不同类型的值</li><li>可以将某个查询的结果赋值给一个变量</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SET @a = (SELECT m1 FROM t1 LIMIT 1);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">前提是查询结果集最多包含一行一列（若为空集，则变量值为null）</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>可以用INTO子句代替SET语句来为变量赋值</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT n1 FROM t1 LIMIT 1 INTO @b;</span></span>
<span class="line"><span style="color:#A6ACCD;">此语句与下效果相同</span></span>
<span class="line"><span style="color:#A6ACCD;">SET @a = (SELECT m1 FROM t1 LIMIT 1);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><ul><li>当结果集的记录中包含多个列时，只能用INTO</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT m1,m2 FROM t1 LIMIT 1 INTO @a,@b;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="存储函数" tabindex="-1">存储函数 <a class="header-anchor" href="#存储函数" aria-hidden="true">#</a></h2><ul><li>存储程序类似于方法</li><li>存储函数有返回值</li></ul><h3 id="创建存储函数" tabindex="-1">创建存储函数 <a class="header-anchor" href="#创建存储函数" aria-hidden="true">#</a></h3><ul><li>语法：</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE FUNCTION 存储函数名称([参数列表])</span></span>
<span class="line"><span style="color:#A6ACCD;">RETURN 返回值类型</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	函数体内容</span></span>
<span class="line"><span style="color:#A6ACCD;">END</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><blockquote><p>若不需要参数，参数列表可省略</p><p>函数体内容中的每条语句都要用分号(;)结尾</p></blockquote><ul><li>示例：</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DELIMITER $   # 将MySQL客户端语句分割符替换为$</span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE FUNCTION avg_score (s VARCHAR(100))</span></span>
<span class="line"><span style="color:#A6ACCD;">RETURN DOUBLE</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	RETURN (SELECT AVG(score) FROM student_score WHERE subject = s);</span></span>
<span class="line"><span style="color:#A6ACCD;">END $</span></span>
<span class="line"><span style="color:#A6ACCD;">DELIMITER ;   # 将MySQL客户端语句分割符替换为;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><blockquote><p>其中DELIMITER $ 意味着将MySQL客户端的语句的分隔符从 ; 替换为 $</p></blockquote><h3 id="存储函数的调用" tabindex="-1">存储函数的调用 <a class="header-anchor" href="#存储函数的调用" aria-hidden="true">#</a></h3><blockquote><p>自定义函数与系统内置函数的使用方法相同</p><p>函数名后加小括号()来调用</p></blockquote><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 调用上定义的avg_score函数</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT avg_score(&#39;MySQL是怎样运行的&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h3 id="查看和删除存储函数" tabindex="-1">查看和删除存储函数 <a class="header-anchor" href="#查看和删除存储函数" aria-hidden="true">#</a></h3><ul><li>查看现有存储函数以及各个存储函数的属性</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW FUNCTION STATUS [LIKE 需要匹配的函数名]</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>查看某个函数是怎样定义的</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW CREATE FUNCTION 函数名</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 如：</span></span>
<span class="line"><span style="color:#A6ACCD;">SHOW CREATE FUNCTION avg_score\G</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><ul><li>删除存储函数</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DROP FUNCTION 函数名</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="函数体的定义" tabindex="-1">函数体的定义 <a class="header-anchor" href="#函数体的定义" aria-hidden="true">#</a></h3><h4 id="在函数体中定义局部变量" tabindex="-1">在函数体中定义局部变量 <a class="header-anchor" href="#在函数体中定义局部变量" aria-hidden="true">#</a></h4><blockquote><p>若想在存储函数的函数体中使用变量，必须提前使用DECLARE语句声明该变量</p><p>在函数体内声明的变量值在该函数体内有效</p></blockquote><p>声明语法</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DECLARE 变量1,变量2,... 数据类型 [DEFAULT 默认值];</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">函数体中的局部变量名不允许加@前缀（除非使用反引号将变量名引起来）</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DELIMITER $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE FUNCTION var_demo()</span></span>
<span class="line"><span style="color:#A6ACCD;">RETURN INT</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	DECLARE c INT;</span></span>
<span class="line"><span style="color:#A6ACCD;">	SET c = 5;</span></span>
<span class="line"><span style="color:#A6ACCD;">	RETURN c;</span></span>
<span class="line"><span style="color:#A6ACCD;">END $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">DELIMITER ;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 也可以直接通过DEFAULT子句来显示的指定局部变量的默认值</span></span>
<span class="line"><span style="color:#A6ACCD;">	DECLARE c INT DEFAULT 5;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h4 id="在函数体中使用用户自定义变量" tabindex="-1">在函数体中使用用户自定义变量 <a class="header-anchor" href="#在函数体中使用用户自定义变量" aria-hidden="true">#</a></h4><blockquote><p>除了局部变量外，也可以直接在函数体中使用用户自定义变量</p></blockquote><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DELIMITER $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE FUNCTION user_defined_var_demo()</span></span>
<span class="line"><span style="color:#A6ACCD;">RETURN INT</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	SET @abc = 10;</span></span>
<span class="line"><span style="color:#A6ACCD;">	RETURN @abc;</span></span>
<span class="line"><span style="color:#A6ACCD;">END $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">DELIMITER ;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 调用此函数</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT @abc;----NULL</span></span>
<span class="line"><span style="color:#A6ACCD;">=&gt; 此时用户自定义变量abc的值为NULL</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT user_defined_var_demo();</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT @abc;----10</span></span>
<span class="line"><span style="color:#A6ACCD;">=&gt; 在调用了user_defined_var_demo()存储函数之后，abc的值就变为了10</span></span>
<span class="line"><span style="color:#A6ACCD;">=&gt; 即使存储函数执行完毕，该存储函数修改过的用户自定义变量的值依然生效</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><p>注意：此处与在函数体中使用DECLARE声明的局部变量有明显区别。</p><h4 id="存储函数的参数" tabindex="-1">存储函数的参数 <a class="header-anchor" href="#存储函数的参数" aria-hidden="true">#</a></h4><blockquote><p>在定义存储函数时，可以指定多个参数，且每个参数都要制定对应的数据类型</p></blockquote><p>就像这样：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">参数名 数据类型</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE FUNCTION avg_score (s VARCHAR(100))</span></span>
<span class="line"><span style="color:#A6ACCD;">RETURN DOUBLE</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	RETURN (SELECT AVG(score) FROM student_score WHERE subject = s);</span></span>
<span class="line"><span style="color:#A6ACCD;">END</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 此处为数据类型为VARCHAR(100)的参数s</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>注意：函数参数不能制定默认值。</p><h4 id="判断语句的编写" tabindex="-1">判断语句的编写 <a class="header-anchor" href="#判断语句的编写" aria-hidden="true">#</a></h4><p>语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">IF 表达式 THEN</span></span>
<span class="line"><span style="color:#A6ACCD;">	语句列表</span></span>
<span class="line"><span style="color:#A6ACCD;">[ELSEIF 表达式 THEN</span></span>
<span class="line"><span style="color:#A6ACCD;">	语句列表]</span></span>
<span class="line"><span style="color:#A6ACCD;">... # 此处可有多个ELSEIF语句</span></span>
<span class="line"><span style="color:#A6ACCD;">[ELSE</span></span>
<span class="line"><span style="color:#A6ACCD;">	语句列表]</span></span>
<span class="line"><span style="color:#A6ACCD;">END IF;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>其中，语句列表中可以包含多条语句。</p><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DELIMITER $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE FUNCTION condition_demo(i INT) # 此函数接受一个INT类型的参数</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	DECLARE result VARCHAR(10);</span></span>
<span class="line"><span style="color:#A6ACCD;">	IF i = 1 THEN</span></span>
<span class="line"><span style="color:#A6ACCD;">		SET result = &#39;结果是1&#39;; # 当参数为1，result就被赋值为“结果是1”</span></span>
<span class="line"><span style="color:#A6ACCD;">	ELSEIF i = 2 THEN</span></span>
<span class="line"><span style="color:#A6ACCD;">		SET result = &#39;结果是2&#39;; # 当参数为2，result就被赋值为“结果是2”</span></span>
<span class="line"><span style="color:#A6ACCD;">	ELSEIF i = 3 THEN</span></span>
<span class="line"><span style="color:#A6ACCD;">		SET result = &#39;结果是3&#39;; # 当参数为3，result就被赋值为“结果是3”</span></span>
<span class="line"><span style="color:#A6ACCD;">	ELSE</span></span>
<span class="line"><span style="color:#A6ACCD;">		SET result = &#39;非法参数&#39;; # 否则result就被赋值为“非法参数”</span></span>
<span class="line"><span style="color:#A6ACCD;">	END IF;</span></span>
<span class="line"><span style="color:#A6ACCD;">	REturn result;</span></span>
<span class="line"><span style="color:#A6ACCD;">END $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">DELIMITER ;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br></div></div><h4 id="循环语句的编写" tabindex="-1">循环语句的编写 <a class="header-anchor" href="#循环语句的编写" aria-hidden="true">#</a></h4><p>MySQL支持三种循环语句的编写，分别为WHILE、REPEAT、LOOP。、</p><ul><li><strong>WHILE循环语句</strong></li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">WHILE 表达式 DO</span></span>
<span class="line"><span style="color:#A6ACCD;">	语句列表</span></span>
<span class="line"><span style="color:#A6ACCD;">END WHILE;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 含义</span></span>
<span class="line"><span style="color:#A6ACCD;">如果表达式为真，则执行语句列表中的语句，否则退出循环</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li><strong>REPEAT循环语句</strong></li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">REPEAT</span></span>
<span class="line"><span style="color:#A6ACCD;">	语句列表</span></span>
<span class="line"><span style="color:#A6ACCD;">UNTIL 表达式 END REPEAT;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 含义</span></span>
<span class="line"><span style="color:#A6ACCD;">先执行语句列表的语句，再判断表达式是否为真，为真则退出循环，否则继续执行</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><ul><li><strong>LOOP循环语句</strong></li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">LOOP</span></span>
<span class="line"><span style="color:#A6ACCD;">	语句列表</span></span>
<span class="line"><span style="color:#A6ACCD;">END LOOP;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 含义</span></span>
<span class="line"><span style="color:#A6ACCD;">一直循环</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 注意</span></span>
<span class="line"><span style="color:#A6ACCD;">此循环没有循环终止的条件！</span></span>
<span class="line"><span style="color:#A6ACCD;">需要把循环终止的条件写在语句列表中，然后使用RETURN语句结束此函数</span></span>
<span class="line"><span style="color:#A6ACCD;">若只是结束循环，可用LEAVE语句在LOOP语句前放置标记，来进行结束</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 示例</span></span>
<span class="line"><span style="color:#A6ACCD;">flag:LOOP</span></span>
<span class="line"><span style="color:#A6ACCD;">	IF i&gt;n THEN</span></span>
<span class="line"><span style="color:#A6ACCD;">		LEAVE flag;</span></span>
<span class="line"><span style="color:#A6ACCD;">	END IF;</span></span>
<span class="line"><span style="color:#A6ACCD;">	...</span></span>
<span class="line"><span style="color:#A6ACCD;">END LOOP flag;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h2 id="存储过程" tabindex="-1">存储过程 <a class="header-anchor" href="#存储过程" aria-hidden="true">#</a></h2><ul><li>存储过程没有返回值</li></ul><h3 id="创建存储过程" tabindex="-1">创建存储过程 <a class="header-anchor" href="#创建存储过程" aria-hidden="true">#</a></h3><p>语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE PROCEDURE 存储过程名称([参数列表])</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	需要执行的语句</span></span>
<span class="line"><span style="color:#A6ACCD;">END</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DELIMITER $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE PROCEDURE t1_operation(m1_value INT,n1_value CHAR(1))</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	SELECT * FROM t1;</span></span>
<span class="line"><span style="color:#A6ACCD;">	INSERT INTO t1(m1,n1) VALUES(m1_value,n1_value);</span></span>
<span class="line"><span style="color:#A6ACCD;">	SELECT * FROM t1;</span></span>
<span class="line"><span style="color:#A6ACCD;">END $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">DELIMITER ;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><h3 id="存储过程的调用" tabindex="-1">存储过程的调用 <a class="header-anchor" href="#存储过程的调用" aria-hidden="true">#</a></h3><ul><li>存储过程没有返回值，需要显示的使用<u>CALL语句</u>来调用存储过程</li></ul><p>语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CALL 存储过程([参数列表]);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 调用t1_operation存储过程</span></span>
<span class="line"><span style="color:#A6ACCD;">CALL t1_operation(4,&#39;d&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br></div></div><h3 id="查看和删除存储过程" tabindex="-1">查看和删除存储过程 <a class="header-anchor" href="#查看和删除存储过程" aria-hidden="true">#</a></h3><ul><li>查看目前服务器已经创建了那些存储过程</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW PROCEDURE STATUS [LIKE 需要匹配的存储过程名称]</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>查看某存储过程的定义语句</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW CREATE PROCEDURE 存储过程名称</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>删除存储过程</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DROP PROCEDURE 存储过程名称</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="存储过程的参数前缀" tabindex="-1">存储过程的参数前缀 <a class="header-anchor" href="#存储过程的参数前缀" aria-hidden="true">#</a></h3><p>存储过程在定义参数的时候可以选择添加一些前缀</p><p>语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">[IN | OUT | INOUT] 参数名 数据类型</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><table><thead><tr><th style="text-align:center;">前缀</th><th style="text-align:center;">实际参数是否必须有变量</th><th>描述</th></tr></thead><tbody><tr><td style="text-align:center;">IN</td><td style="text-align:center;">否</td><td>用于调用者向存储过程传递参数，若IN参数在存储过程中被修改，则调用者不可见</td></tr><tr><td style="text-align:center;">OUT</td><td style="text-align:center;">是</td><td>用于把存储过程运行过程中产生的数据赋值给OUT参数，存储过程执行结束后，调用者可以访问OUT参数</td></tr><tr><td style="text-align:center;">INOUT</td><td style="text-align:center;">是</td><td>综合IN和OUT的特点，既可以用于调用者向存储过程传递参数，也可以用于存放存储过程中产生的数据以供调用者使用</td></tr></tbody></table><h2 id="游标简介" tabindex="-1">游标简介 <a class="header-anchor" href="#游标简介" aria-hidden="true">#</a></h2><p><strong>游标</strong>是一个存储在MySQL服务器上的数据库查询，它不是一条select语句，而是被该语句所检索出来的结果集。</p><h3 id="创建游标" tabindex="-1">创建游标 <a class="header-anchor" href="#创建游标" aria-hidden="true">#</a></h3><ul><li>在创建游标的时候，需要指定与游标关联的查询语句</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DECLARE 游标名称 CURSOR FOR 查询语句;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>示例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 定义一个存储过程</span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE PROCEDURE cursor_demo()</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	DECLARE t1_record_cursor CURSOR FOR SELECT m1,n1 FROM t1;</span></span>
<span class="line"><span style="color:#A6ACCD;">END</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 这样名为t1_record_cursor的游标就创建完成了</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><blockquote><p>注意：</p><p>若在存储过程中也有声明局部变量的语句，则创建游标的语句一定要放在局部变量声明的后面。</p></blockquote><h3 id="打开和关闭游标" tabindex="-1">打开和关闭游标 <a class="header-anchor" href="#打开和关闭游标" aria-hidden="true">#</a></h3><ul><li>需要手动打开和关闭游标</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">OPEN 游标名称;</span></span>
<span class="line"><span style="color:#A6ACCD;">CLOSE 游标名称;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><ul><li><p>打开游标</p><ul><li>意味着执行查询语句，使之前声明的游标与查询语句的结果集关联起来。</li></ul></li><li><p>关闭游标</p><ul><li>意味着释放与该游标相关的资源，所以一旦使用完了游标，就要将其关闭。</li><li>若不用CLOSE，则在存储过程的END语句执行完之后会自动关闭游标。</li></ul></li></ul><h3 id="通过游标获取记录" tabindex="-1">通过游标获取记录 <a class="header-anchor" href="#通过游标获取记录" aria-hidden="true">#</a></h3><p>获取记录的语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">FETCH 游标名 INTO 变量1,变量2,...,变量n;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">此句的意思是</span></span>
<span class="line"><span style="color:#A6ACCD;">		把当前游标对应记录的各列的值依次赋值给INTO后的各个变量</span></span>
<span class="line"><span style="color:#A6ACCD;">注意： 此语句每次只获取一条记录，要获取多条记录需要加循环</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE PROCEDURE cursor_demo()</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	DECLARE m_value INT;</span></span>
<span class="line"><span style="color:#A6ACCD;">	DECLARE n_value CHAR(1);</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span></span>
<span class="line"><span style="color:#A6ACCD;">	DECLARE t1_record_cursor CURSOR FOR SELECT m1,n1 FROM t1;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span></span>
<span class="line"><span style="color:#A6ACCD;">	OPEN t1_record_cursor;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span></span>
<span class="line"><span style="color:#A6ACCD;">	FETCH t1_record_cursor INTO m_value,n_value;</span></span>
<span class="line"><span style="color:#A6ACCD;">	SELECT m_value,n_value;</span></span>
<span class="line"><span style="color:#A6ACCD;">	</span></span>
<span class="line"><span style="color:#A6ACCD;">	CLOSE t1_record_cursor;</span></span>
<span class="line"><span style="color:#A6ACCD;">END $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h3 id="错误时的执行策略" tabindex="-1">错误时的执行策略 <a class="header-anchor" href="#错误时的执行策略" aria-hidden="true">#</a></h3><blockquote><p>在FETCH语句获取不到记录的时候默认会停止存储函数或存储过程的执行，并报错</p><p>但有时候我们不希望程序停止运行</p><p>则可以在存储函数或存储过程中事先声明一种针对某种错误的处理方式，这样在服务器执行期间，若遇到了错误，不会停止并报错，而是采用事先声明的错误处理方式取处理。</p></blockquote><p>在遇到FETCH语句获取不到记录的情况时，可使用：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DECLARE CONTINUE HANDLER FOR NOT FOUND 处理语句;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="触发器" tabindex="-1">触发器 <a class="header-anchor" href="#触发器" aria-hidden="true">#</a></h2><p>MySQL触发器是与表关联的数据库存储程序，用于响应关联表中发生的事件（例如插入，更新或删除）而自动调用。</p><h3 id="创建触发器" tabindex="-1">创建触发器 <a class="header-anchor" href="#创建触发器" aria-hidden="true">#</a></h3><p>语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE TRIGGER 触发器名</span></span>
<span class="line"><span style="color:#A6ACCD;">{BEFORE|AFTER}</span></span>
<span class="line"><span style="color:#A6ACCD;">{INSERT|DELETE|UPDATE}</span></span>
<span class="line"><span style="color:#A6ACCD;">ON 表名</span></span>
<span class="line"><span style="color:#A6ACCD;">FOR EACH ROW</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	触发器内容</span></span>
<span class="line"><span style="color:#A6ACCD;">END</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><table><thead><tr><th style="text-align:center;">名称</th><th>描述</th></tr></thead><tbody><tr><td style="text-align:center;">BEFORE</td><td>表示在具体的语句执行之前就开始执行触发器的内容</td></tr><tr><td style="text-align:center;">AFTER</td><td>表示在具体的语句执行之后才开始执行触发器的内容</td></tr></tbody></table><blockquote><p>因为MySQL服务器会对某条语句影响的所有记录依次调用用户自定义的触发器内容，所以针对每一条受影响的记录，我们需要一种方式来访问该纪录中的内容。</p><p>MySQL提供了NEW和OLD这两词来分别代表新纪录和旧记录。</p></blockquote><p>NEW和OLD在各语句中的含义：</p><ul><li>对于INSERT语句设置的触发器，NEW代表准备插入的记录，OLD无效。</li><li>对于DELETE语句...，OLD代表删除前的记录，NEW无效。</li><li>对于UPDATE语句...，NEW代表修改后的记录，OLD代表修改前的记录。</li></ul><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DELIMITER $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE TRIGGER bi_t1</span></span>
<span class="line"><span style="color:#A6ACCD;">BEFORE INSERT ON t1</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	IF NEW.m1 &lt; 1 THEN</span></span>
<span class="line"><span style="color:#A6ACCD;">		SET NEW.m1 = 1;</span></span>
<span class="line"><span style="color:#A6ACCD;">	ELSEIF NEW.m1 &gt; 10 THEN</span></span>
<span class="line"><span style="color:#A6ACCD;">		SET NEW.m1 = 10;</span></span>
<span class="line"><span style="color:#A6ACCD;">	END IF;</span></span>
<span class="line"><span style="color:#A6ACCD;">END $</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">DELIMITER ;</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 上方代码的解释</span></span>
<span class="line"><span style="color:#A6ACCD;">对t1表定义了一个名为bi_t1的触发器，意思时在对t1表插入新纪录之前，对准备插入的每一条记录都会执行BEGIN...END之间的语句，“NEW.列名”表示当前待插入记录指定列的值。</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br></div></div><h3 id="查看和删除触发器" tabindex="-1">查看和删除触发器 <a class="header-anchor" href="#查看和删除触发器" aria-hidden="true">#</a></h3><ul><li>查看当前数据库中定义的所有触发器</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW TRIGGERS;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>查看某个触发器的定义语句</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW CREATE TRIGGER 触发器名</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>删除触发器</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DROP TRIGGER 触发器名</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="注意事项-2" tabindex="-1">注意事项 <a class="header-anchor" href="#注意事项-2" aria-hidden="true">#</a></h3><ol><li>触发器内容中不能有输出结果集的语句。</li><li>触发器内容中NEW代表记录的列的值可以被更改，OLD代表记录的列的值无法更改。</li><li>在BEFORE触发器中，使用“SET NEW.列名 = 某个值”的形式来更改待插入记录或者待更新记录的某个值的列，但是这种操作不能再AFTER触发器中使用，因为再执行AFTER触发器的内容时记录已经被插入完成或者更新完成了。</li></ol><h2 id="事件" tabindex="-1">事件 <a class="header-anchor" href="#事件" aria-hidden="true">#</a></h2><p>MySQL中的事件 (<strong>event</strong>)是用于执行定时或周期性的任务，事件由一个特定的线程来管理的，也就是所谓的事件调度器，但是事件不能直接调用。</p><h3 id="创建事件" tabindex="-1">创建事件 <a class="header-anchor" href="#创建事件" aria-hidden="true">#</a></h3><p>语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE EVENT 事件名</span></span>
<span class="line"><span style="color:#A6ACCD;">ON SCHEDULE</span></span>
<span class="line"><span style="color:#A6ACCD;">{</span></span>
<span class="line"><span style="color:#A6ACCD;">	AT 某个确定的时间点|</span></span>
<span class="line"><span style="color:#A6ACCD;">	EVERY 期望的时间间隔 [STARTS 开始日期和时间][END 结束日期和时间]</span></span>
<span class="line"><span style="color:#A6ACCD;">}</span></span>
<span class="line"><span style="color:#A6ACCD;">DO</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	具体的语句</span></span>
<span class="line"><span style="color:#A6ACCD;">END</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>事件支持两种类型的自动执行方式：</p><p>​ ①在某个确定的时间点执行</p><p>​ ②每隔一段时间执行一次</p><p><strong>===&gt;&gt;&gt;在某个确定的时间执行</strong></p><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE EVENT insert_t1_event</span></span>
<span class="line"><span style="color:#A6ACCD;">ON SCHEDULE</span></span>
<span class="line"><span style="color:#A6ACCD;">AT &#39;2022-2-2 12:12:12&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">DO</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	INSERT INTO t1(m1,n1) VALUES(6,&#39;f&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;">END</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 表示在2022-2-2 12:12:12执行</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE EVENT insert_t1_event</span></span>
<span class="line"><span style="color:#A6ACCD;">ON SCHEDULE</span></span>
<span class="line"><span style="color:#A6ACCD;">AT DATE_ADD(NOW(),INTERVAL 2 DAY)</span></span>
<span class="line"><span style="color:#A6ACCD;">DO</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	INSERT INTO t1(m1,n1) VALUES(6,&#39;f&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;">END</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 表示在当前时间的两天后执行</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p><strong>===&gt;&gt;&gt;每隔一段时间执行一次</strong></p><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">CREATE EVENT insert_t1_event</span></span>
<span class="line"><span style="color:#A6ACCD;">ON SCHEDULE</span></span>
<span class="line"><span style="color:#A6ACCD;">EVERY 1 HOUR</span></span>
<span class="line"><span style="color:#A6ACCD;">DO</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	INSERT INTO t1(m1,n1) VALUES(6,&#39;f&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;">END</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 表示每隔一个小时执行一次</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">CREATE EVENT insert_t1_event</span></span>
<span class="line"><span style="color:#A6ACCD;">ON SCHEDULE</span></span>
<span class="line"><span style="color:#A6ACCD;">EVERY 1 HOUR STARTS &#39;2022-2-2 12:12:12&#39; ENDS &#39;2023-2-2 12:12:12&#39;</span></span>
<span class="line"><span style="color:#A6ACCD;">DO</span></span>
<span class="line"><span style="color:#A6ACCD;">BEGIN</span></span>
<span class="line"><span style="color:#A6ACCD;">	INSERT INTO t1(m1,n1) VALUES(6,&#39;f&#39;);</span></span>
<span class="line"><span style="color:#A6ACCD;">END</span></span>
<span class="line"><span style="color:#A6ACCD;">-- 表示自2022-2-2 12:12:12开始到2023-2-2 12:12:12为止的时间段，每隔一个小时执行一次</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><p>注意：</p><p>​ 定时执行事件的功能需要开启后才能正常使用，如</p><p>​ SET GLOBAL event_scheduler = ON;</p><h3 id="查看和删除事件" tabindex="-1">查看和删除事件 <a class="header-anchor" href="#查看和删除事件" aria-hidden="true">#</a></h3><ul><li>查看当前数据库中定义的所有事件</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW EVENTS;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>查看某事件的定义语句</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SHOW CREATE EVENT 事件名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>删除事件</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">DROP EVENT 事件名;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h1 id="备份与恢复" tabindex="-1">备份与恢复 <a class="header-anchor" href="#备份与恢复" aria-hidden="true">#</a></h1><h2 id="mysqldump" tabindex="-1">mysqldump <a class="header-anchor" href="#mysqldump" aria-hidden="true">#</a></h2><h3 id="备份数据" tabindex="-1">备份数据 <a class="header-anchor" href="#备份数据" aria-hidden="true">#</a></h3><p>在bin目录下，有一个名为mysqldump的可执行文件是用来备份数据的。</p><p><strong>⚪备份指定数据库的指定表</strong></p><p>命令如下：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">mysqldump [其他选项] 数据库名 [表1名,表2名,表3名,...]</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">mysqldump -uroot -hlocalhost -p xiaohaizi student_score &gt; student_score.sql</span></span>
<span class="line"><span style="color:#A6ACCD;">Enter password:******</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">解释：</span></span>
<span class="line"><span style="color:#A6ACCD;">mysqldump本身时一个客户端程序，在进行备份时需要与服务器进行通信，故在此执行-u（用户名）、-h（主机）和-p（密码）参数</span></span>
<span class="line"><span style="color:#A6ACCD;">由于mysqldump的输出内容较多，建议把输出内容重定向到某个文件中，而&gt; student_score.sql表示把命令的执行结果重定向到一个名为student_score.sql的文件中（此处为相对路径）</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">此后若student_score表被误删，或想建一个相同的表，可以执行这个student_score.sql文件中的语句。</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p><strong>⚪备份指定数据库中的所有表</strong></p><p>命令如下：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">mysqldump [其他选项] --databases 数据库1名,数据库2名,数据库3名,...</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 备份xiaohaizi、dahaizi这两个数据库下的所有表</span></span>
<span class="line"><span style="color:#A6ACCD;">mysqldump -uroot -hlocalhost -p --databases xiaohaizi dahaizi</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p><strong>⚪备份所有数据库下的所有表</strong></p><p>命令如下：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">mysqldump [其他选项] --all-databases</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">mysqldump -uroot -hlocalhost -p --all-databases</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h3 id="恢复数据" tabindex="-1">恢复数据 <a class="header-anchor" href="#恢复数据" aria-hidden="true">#</a></h3><p>可以通过SOURCE语句来执行备份文件中的语句</p><p>方法：只需要将备份文件的路径放在SOURCE单词后，即可执行</p><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 先删除</span></span>
<span class="line"><span style="color:#A6ACCD;">DROP TABLE student_score;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 检查是否被删除</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT * FROM student_score;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 恢复</span></span>
<span class="line"><span style="color:#A6ACCD;">SOURCE C:\Users\xiaohaizi\student_score.sql;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><h2 id="以文本形式导出或导入" tabindex="-1">以文本形式导出或导入 <a class="header-anchor" href="#以文本形式导出或导入" aria-hidden="true">#</a></h2><h3 id="导出数据" tabindex="-1">导出数据 <a class="header-anchor" href="#导出数据" aria-hidden="true">#</a></h3><p>语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT ... INTO OUTFILE &#39;文件路径&#39; [导出选项]</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>常用的导出选项如下</p><ul><li><p>FILEDS TERMINATED BY</p><ul><li>表示列分隔符，也就是各列的值之间使用什么符号进行分隔，默认以&#39;\t&#39;分隔。</li><li>如：FILEDS TREMINATED BY &#39;,&#39; 表示以逗号分隔。</li></ul></li><li><p>FILEDS [OPTIONALLY] ENCLOSED BY</p><ul><li>表示列引用符，也就是每个列的值被什么符号包裹起来，默认是空字符串&#39;&#39;。 若加上OPTIONALLY则只会作用于字符串类型的列。</li><li>如：FILEDS ENCLOSED BY &#39; &quot; &#39; 表示每个列的值都将被双引号引起来。</li></ul></li><li><p>LINES STARTING BY</p><ul><li>表示行开始符，也就是每一行以什么符号开始，默认是空字符串&#39;&#39;。</li><li>如：LINES STARTING BY &#39;%%&#39; 表示每行都以两个百分号开头。</li></ul></li><li><p>LINES TERMINATED BY</p><ul><li>表示行结束符，也就是每一行以什么符号结尾，默认是换行符&#39;\n&#39;。</li><li>如：LINES TERMINATED BY &#39;$$&#39; 表示每行都以两个美元符号结尾。</li></ul></li></ul><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">SELECT * FROM student_score INTO OUTFILE &#39;C/ProgramData/MySQL/MySQL Server 8.0/Uploads/student_score1.txt&#39; FIELDS OPTIONALLY ENCLOSED BY &#39;&quot;&#39; LINES STARTING BY &#39;&gt;_&gt;&#39;;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">解释：</span></span>
<span class="line"><span style="color:#A6ACCD;">将student_score表导出目录为‘...’的文件名为‘student_score1.txt’</span></span>
<span class="line"><span style="color:#A6ACCD;">且导出文件的每行以‘&gt;_&gt;’开头</span></span>
<span class="line"><span style="color:#A6ACCD;">且subject列的值被双引号引起来（因为subject列时字符串类型）</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p>注意：使用SELECT ... INTO OUTFILE语句导出的数据会被存储到运行服务器程序的主机上。</p><h3 id="导入数据" tabindex="-1">导入数据 <a class="header-anchor" href="#导入数据" aria-hidden="true">#</a></h3><p>语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">LOAD DATA [LOCAL] INFILE &#39;文件路径&#39; INTO TABLE 表名 [导入选项]</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>若填入LOCAL，则表名要导入的数据文件在运行客户端的主机中，否则就在运行服务器的主机中。</li><li>常用的导入选项和SELECT ... INTO OUTFILE语句中的导出选项类似。</li></ul><p>案例：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 先把student_score表中的记录删除</span></span>
<span class="line"><span style="color:#A6ACCD;">DELETE FROM student_score;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 再导入数据</span></span>
<span class="line"><span style="color:#A6ACCD;">LOAD DATA INFILE &#39;C/ProgramData/MySQL/MySQL Server 8.0/Uploads/student_score1.txt&#39; INTO TABLE student_score;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><h1 id="事务" tabindex="-1">事务 <a class="header-anchor" href="#事务" aria-hidden="true">#</a></h1><h2 id="简介" tabindex="-1">简介 <a class="header-anchor" href="#简介" aria-hidden="true">#</a></h2><p>事务：是一组操作的集合，它是一个不可分割的工作单位，事务会把所有的操作作为一个整体一起向系统提交或撤销操作请求，即这些操作要么同时成功，要么同时失败。</p><p>​ 事务主要用于处理操作量大，复杂度高的数据。</p><h2 id="事务的操作" tabindex="-1">事务的操作 <a class="header-anchor" href="#事务的操作" aria-hidden="true">#</a></h2><ul><li>查看/设置事务的提交方式</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查看事务的提交方式，1为自动提交，0为手动提交，默认为1</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT @@autocommit;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 设置事务的提交方式</span></span>
<span class="line"><span style="color:#A6ACCD;">SET @@autocommit = 0;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>除上一种方法控制事务，也可用下方法显式开启事务</p><ul><li>开启事务</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">START TRANSACTION 或 BEGIN;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><ul><li>提交事务</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">COMMIT;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>若事务的提交方式为手动提交，则语句执行完毕后需要执行COMMIT语句提交事务。</p><ul><li>回滚事务</li></ul><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">ROLLBACK;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><p>若语句出错或有不满意的结果，可执行回滚事务撤回本次操作。</p><h2 id="事务的四大特性" tabindex="-1">事务的四大特性 <a class="header-anchor" href="#事务的四大特性" aria-hidden="true">#</a></h2><ul><li>原子性（Atomicity） <ul><li>事务是不可分割的最小操作单元，要么全部成功，要么全部失败。</li></ul></li><li>一致性（Consistency） <ul><li>事务完成时，必须使所有的数据都保持一致状态。</li></ul></li><li>隔离性（Isolation） <ul><li>数据库系统提供的隔离机制，保证事务在不受外部并发操作影响的独立环境下运行。</li></ul></li><li>持久性（Durability） <ul><li>事务一旦提交或回滚，它对数据库中的数据的改变就是永久的。</li></ul></li></ul><h2 id="并发事务问题" tabindex="-1">并发事务问题 <a class="header-anchor" href="#并发事务问题" aria-hidden="true">#</a></h2><table><thead><tr><th style="text-align:center;">问题</th><th>描述</th></tr></thead><tbody><tr><td style="text-align:center;">脏读</td><td>一个事务读到另一个事务还没有提交的数据</td></tr><tr><td style="text-align:center;">不可重复读</td><td>一个事务先后读取同一条记录，但两次读取的数据不同</td></tr><tr><td style="text-align:center;">幻读</td><td>一个事务按照条件查询数据时，没有对应的数据行，但在插入数据时，又发现这行数据已经存在</td></tr></tbody></table><h2 id="事务隔离级别" tabindex="-1">事务隔离级别 <a class="header-anchor" href="#事务隔离级别" aria-hidden="true">#</a></h2><table><thead><tr><th style="text-align:center;">隔离级别</th><th style="text-align:center;">脏读</th><th style="text-align:center;">不可重复读</th><th style="text-align:center;">幻读</th></tr></thead><tbody><tr><td style="text-align:center;">Read uncommitted</td><td style="text-align:center;">√</td><td style="text-align:center;">√</td><td style="text-align:center;">√</td></tr><tr><td style="text-align:center;">Read committed</td><td style="text-align:center;">×</td><td style="text-align:center;">√</td><td style="text-align:center;">√</td></tr><tr><td style="text-align:center;">Repeatable Read(默认)</td><td style="text-align:center;">×</td><td style="text-align:center;">×</td><td style="text-align:center;">√</td></tr><tr><td style="text-align:center;">Serializable</td><td style="text-align:center;">×</td><td style="text-align:center;">×</td><td style="text-align:center;">×</td></tr></tbody></table><p>注：越往下级别越高，但性能越差。</p><p>​ 默认仅代表MySQL</p><p>相关语法：</p><div class="language-mysql line-numbers-mode"><button title="Copy Code" class="copy"></button><span class="lang">mysql</span><pre class="shiki material-theme-palenight" tabindex="0"><code><span class="line"><span style="color:#A6ACCD;">-- 查看事务隔离级别</span></span>
<span class="line"><span style="color:#A6ACCD;">SELECT @@TRANSACTION_ISOLATION;</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span>
<span class="line"><span style="color:#A6ACCD;">-- 设置事务隔离级别</span></span>
<span class="line"><span style="color:#A6ACCD;">SET [SESSION|GLOBAL] TRANSACTION ISOLATION LEVEL {READ UNCOMMITTED|READ COMMITTED|REPEATABLE READ|SERIALIZABLE}</span></span>
<span class="line"><span style="color:#A6ACCD;">注：SESSION对当前窗口有效，GLOBAL对全局窗口有效</span></span>
<span class="line"><span style="color:#A6ACCD;"></span></span></code></pre><div class="line-numbers-wrapper" aria-hidden="true"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div></div></div></main><!--[--><!--]--><footer class="VPDocFooter" data-v-c5936a1e data-v-2813752b><div class="edit-info" data-v-2813752b><div class="edit-link" data-v-2813752b><a class="VPLink link edit-link-button" href="https://github.com/vuejs/vitepress/edit/main/docs/MySqlStudy/MySQL.md" target="_blank" rel="noreferrer" data-v-2813752b data-v-a8b5c975><!--[--><svg xmlns="http://www.w3.org/2000/svg" viewbox="0 0 24 24" class="edit-link-icon" data-v-2813752b><path d="M18,23H4c-1.7,0-3-1.3-3-3V6c0-1.7,1.3-3,3-3h7c0.6,0,1,0.4,1,1s-0.4,1-1,1H4C3.4,5,3,5.4,3,6v14c0,0.6,0.4,1,1,1h14c0.6,0,1-0.4,1-1v-7c0-0.6,0.4-1,1-1s1,0.4,1,1v7C21,21.7,19.7,23,18,23z"></path><path d="M8,17c-0.3,0-0.5-0.1-0.7-0.3C7,16.5,6.9,16.1,7,15.8l1-4c0-0.2,0.1-0.3,0.3-0.5l9.5-9.5c1.2-1.2,3.2-1.2,4.4,0c1.2,1.2,1.2,3.2,0,4.4l-9.5,9.5c-0.1,0.1-0.3,0.2-0.5,0.3l-4,1C8.2,17,8.1,17,8,17zM9.9,12.5l-0.5,2.1l2.1-0.5l9.3-9.3c0.4-0.4,0.4-1.1,0-1.6c-0.4-0.4-1.2-0.4-1.6,0l0,0L9.9,12.5z M18.5,2.5L18.5,2.5L18.5,2.5z"></path></svg> Edit this page on GitHub<!--]--><!----></a></div><!----></div><div class="prev-next" data-v-2813752b><div class="pager" data-v-2813752b><!----></div><div class="pager" data-v-2813752b><a class="pager-link next" href="../whyVitePress/index.html" data-v-2813752b><span class="desc" data-v-2813752b>下一篇</span><span class="title" data-v-2813752b>介绍</span></a></div></div></footer><!--[--><!--]--></div></div></div></div></div><footer class="VPFooter has-sidebar" data-v-93a960b4 data-v-d24360a6><div class="container" data-v-d24360a6><p class="message" data-v-d24360a6>Released under the MIT License.</p><p class="copyright" data-v-d24360a6>Copyright © 2023-present 孙飞</p></div></footer><!--[--><!--]--></div></div>
    <script>__VP_HASH_MAP__ = JSON.parse("{\"cssstudy_cssdaoying.md\":\"b5a55bb5\",\"html_rearrange-and-redraw.md\":\"79b82326\",\"phototips_1.md\":\"5442fcc8\",\"cssstudy_boxmodel.md\":\"a39c296d\",\"jsstudy_fd-and-jieliu.md\":\"82484a67\",\"article_2022.5.2.md\":\"09de87d8\",\"whyvitepress_index.md\":\"3a2552ae\",\"html_semantictagging.md\":\"fc6888d6\",\"index.md\":\"428f6a9b\",\"dailylife_2023-03.md\":\"c3a9e15d\",\"phototips_2.md\":\"03ecbbde\",\"article_index.md\":\"33d0c092\",\"jslibrary_gsap.md\":\"afb98632\",\"cssstudy_boxjuzhong.md\":\"76f00e08\",\"whyvitepress_start.md\":\"9a284dac\",\"toolweb_index.md\":\"1046e2cd\",\"vscodechajian_index.md\":\"b3548914\",\"vuestudy_1.md\":\"013d73d7\",\"jsstudy_konweltype.md\":\"5e5dbe13\",\"html_index.md\":\"02904cef\",\"cssstudy_flex.md\":\"1983cb54\",\"jsstudy_array.md\":\"93ea04d7\",\"cssstudy_cssoptimization.md\":\"ee5e6100\",\"about_index.md\":\"b2a49978\",\"jsstudy_3-7-objectdeconstruction.md\":\"38a437aa\",\"web3d_three.js.md\":\"f20abcae\",\"vuestudy_vue.md\":\"6905633f\",\"mysqlstudy_mysql.md\":\"113ea88e\"}")</script>
    <script type="module" async src="/Blogs/assets/app.2f47ee87.js"></script>
    
  </body>
</html>